If I have two forms, based on different base classes (say, Form and ModelForm), but I want to use a few fields in both, can I reuse them in a DRY way?
Consider the following scenario:
class AfricanSwallowForm(forms.ModelForm):
airspeed_velocity = forms.IntegerField(some_important_details_here)
is_migratory = forms.BooleanField(more_important_details)
class Meta:
model = AfricanBird
class EuropeanSwallowForm(forms.Form):
airspeed_velocity = forms.IntegerField(some_important_details_here)
is_migratory = forms.BooleanField(more_important_details)
....is there a way I can just reuse the fields airspeed_velocity and is_migratory? Imagine I have a couple dozen of these type of forms. The code will be soaking if I write these over and over again.
(Assume, for the purposes of this question, that I can't or won't turn airspeed_velocity and is_migratory into fields of the model AfricanBird.)
You could use multiple inheritance aka mixins, to factor out the fields that are used in both Form and ModelForm.
class SwallowFormFields:
airspeed_velocity = forms.IntegerField( ... )
is_migratory = forms.BooleanField( ... )
class AfricanSwallowForm(forms.ModelForm, SwallowFormFields):
class Meta:
model = AfricanBird
class EuropeanSwallowForm(forms.Form, SwallowFormFields):
pass
UPDATE:
Since this does not work with Django metaprogramming, you either need to create a custom __init__ constructor that adds the inherited fields to the object's fields list or you can add the references explicitly inside the class definition:
class SwallowFormFields:
airspeed_velocity = forms.IntegerField()
is_migratory = forms.BooleanField()
class AfricanSwallowForm(forms.ModelForm):
airspeed_velocity = SwallowFormFields.airspeed_velocity
is_migratory = SwallowFormFields.is_migratory
class Meta:
model = AfricanSwallow
class EuropeanSwallowForm(forms.Form):
airspeed_velocity = SwallowFormFields.airspeed_velocity
is_migratory = SwallowFormFields.is_migratory
UPDATE:
Of course you don't have to nest your shared fields into a class -- you could also simply define them as globals ...
airspeed_velocity = forms.IntegerField()
is_migratory = forms.BooleanField()
class AfricanSwallowForm(forms.ModelForm):
airspeed_velocity = airspeed_velocity
is_migratory = is_migratory
class Meta:
model = AfricanSwallow
class EuropeanSwallowForm(forms.Form):
airspeed_velocity = airspeed_velocity
is_migratory = is_migratory
UPDATE:
Okay, if you really want to DRY to the max, you have to go with the metaclasses.
So here is how you may do it:
from django.forms.models import ModelForm, ModelFormMetaclass
from django.forms.forms import get_declared_fields, DeclarativeFieldsMetaclass
from django.utils.copycompat import deepcopy
class MixinFormMetaclass(ModelFormMetaclass, DeclarativeFieldsMetaclass):
def __new__(cls, name, bases, attrs):
# default __init__ that calls all base classes
def init_all(self, *args, **kwargs):
for base in bases:
super(base, self).__init__(*args, **kwargs)
attrs.setdefault('__init__', init_all)
# collect declared fields
attrs['declared_fields'] = get_declared_fields(bases, attrs, False)
# create the class
new_cls = super(MixinFormMetaclass, cls).__new__(cls, name, bases, attrs)
return new_cls
class MixinForm(object):
__metaclass__ = MixinFormMetaclass
def __init__(self, *args, **kwargs):
self.fields = deepcopy(self.declared_fields)
You can now derive your collections of formfields from MixinForm like this:
class SwallowFormFields(MixinForm):
airspeed_velocity = forms.IntegerField()
is_migratory = forms.BooleanField()
class MoreFormFields(MixinForm):
is_endangered = forms.BooleanField()
Then add them to the list of base classes like this:
class EuropeanSwallowForm(forms.Form, SwallowFormFields, MoreFormFields):
pass
class AfricanSwallowForm(forms.ModelForm, SwallowFormFields):
class Meta:
model = AfricanSwallow
So what does it do?
The metaclass collects all the fields declared in your MixinForm
It then adds custom __init__ constructors, to make sure that the __init__ method of the MixinForm gets magically called. (Otherwise you would have to call it explicitly.)
MixinForm.__init__ copies the declared fields int the field attribute
Please note that I am neither a Python guru nor a django developer, and that metaclasses are dangerous. So if you encounter weird behaviour better stick with the more verbose approach above :)
Good Luck!
How about a factory-style approach?
def form_factory(class_name, base, field_dict):
always_has = {
'airspeed_velocity': forms.IntegerField(some_important_details_here),
'is_migratory': forms.BooleanField(more_important_details)
}
always_has.update(field_dict)
return type(class_name, (base,), always_has)
def meta_factory(form_model):
class Meta:
model = form_model
return Meta
AfricanSwallowForm = form_factory('AfricanSwallowForm', forms.ModelForm, {
'other' = forms.IntegerField(some_important_details_here),
'Meta': meta_factory(AfricanBird),
})
EuropeanSwallowForm = form_factory('EuropeanSwallowForm', forms.Form, {
'and_a_different' = forms.IntegerField(some_important_details_here),
})
For that matter, you could modify the factory function here to look into an existing form class and pick out the attributes you want, so that you don't lose the declarative syntax...
class SwallowForm(forms.Form):
airspeed_velocity = forms.IntegerField()
is_migratory = forms.BooleanField()
class AfricanSwallowForm(forms.ModelForm, SwallowForm):
class Meta:
model = AfricanSwallow
class EuropeanSwallowForm(forms.Form, SwallowForm):
...
Should work.
I have some long running code that works and has the fields attr that looks like this.
languages_field = forms.ModelMultipleChoiceField(
queryset=Language.objects.all(),
widget=forms.CheckboxSelectMultiple,
required=False
)
class FooLanguagesForm(forms.ModelForm):
languages = languages_field
class Meta:
model = Foo
fields = ('languages', )
Notice that I still use the fields tuple in Meta. The field shows up in the fields dict on the form instance whether or not it is in the Meta.fields.
I have a regular form that uses this pattern as well:
genres_field = forms.ModelMultipleChoiceField(
queryset=blah,
widget=forms.CheckboxSelectMultiple,
#required=False,
)
class FooGenresForm(forms.Form):
genres = genres_field
I see that my fields dict is working.
In [6]: f = FooLanguagesForm()
In [7]: f.fields
Out[7]: {'languages': <django.forms.models.ModelMultipleChoiceField object at 0x1024be450>}
In [8]: f2 = FooGenresForm()
In [9]: f2.fields
Out[9]: {'genres': <django.forms.models.ModelMultipleChoiceField object at 0x1024be3d0>}
Create a subclass of the IntegerField
class AirspeedField(forms.IntegerField):
def __init__():
super(AirspeedField, self).__init__(some_important_details_here)
I have just made a snippet that resolves this issue in a DRY way:
https://djangosnippets.org/snippets/10523/
It uses crispy-form, but the same idea can be used without crispy-forms. The idea is to use multiple forms under the same form tag.
Related
I have a few Serializers that share a few fields like meta_id, category_id and so on.
Obviously I could just declare these on the serializer as a SerializerMethodField individually but I would like to find a way to reuse the logic, either with a Mixin, Decorator, or inheritance.
How can I declare a base serializer and inherit it— while still inheriting from serializers.ModelSerializer? So that I can reuse the get_meta_id and make sure it shows up in the fields?
class Foo(serializers.ModelSerializer, somethingHere?):
meta_id = Serializers.SerializerMethodField()
class Meta:
model = Foo
fields = [...]
def get_meta_id(self, obj):
...
Is it possible to just pass two parameters into the class
You can crete a Base serializer and use inheritence for other serializers.Like that:
class BaseSerializer(serializers.Serializer):
#your codes and extra fields
test_field = serializer.SerializerMethodField()
def get_test_field(self.obj):
return 'test' # or you can use obj instance here
class ExtendedSerializer(BaseSerializer,serializers.ModelSerializer):
#your extra fields
class Meta:
fields = BaseSerializer.Meta.fields + () # you can add your additional fields here
class BaseSerializer(serializers.ModelSerializer): # The BaseSerializer class inherit everthing from ModelSerializer
class Meta(serializers.ModelSerializer.Meta): # The BaseSerializer.Meta inherits everything from ModelSerializer.Meta
def get_meta_id(self, obj):
return self.meta_id
class Foo(BaseSerializer):
meta_id = Serializers.SerializerMethodField()
class Meta(BaseSerializer.Meta): # Here you should get the ModelSerializer.Meta + your custom get_meta_id
model = Foo
fields = [...]
I am trying to write a simple CRUD application. I have several pretty similar model classes and one serializer:
class TestModelSerializer(serializers.ModelSerializer):
queryset = None
#classmethod
def initialize(cls, queryset, context_class, **kwargs):
_srlz = cls()
_srlz.Meta.model = context_class
_srlz.Meta.fields = '__all__'
_srlz.queryset = queryset
_srlz = cls(_srlz.queryset, **kwargs)
return _srlz
class Meta:
model = None
fields = ''
so I can serialize my classes, calling initialize function like this:
_q = Foo.objects.all()
_srlz = TestModelSerializer.initialize(_q, Foo, many = True)
_q2 = Bar.objects.all()
_srlz2 = TestModelSerializer.initialize(_q2, Bar, many = True)
and so on.
But I have faced to one problem. Sometimes my classes are in hard One-to-Many relation (composition):
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo, on_delete = models.CASCADE)
When I serialize Foo class I want to serialize a list of related Bar classes as well and nest result data to Foo's serializer data.
I am not intended to write custom serializer for each of these classes but instead try to implement some generic approach.
I decided to make some experiments and finally create an interface that provides me several methods for my collection items:
class Foo(models.Model):
bars = iCollection
pass
Now when instantiate a serializer I can get all fields of my model class that have iCollection type.
The question is how can I add them to my serializer?
I tried to write an abstract fabric and create a serializer class using type(), but this approach is too ugly, I suppose there should be much more easier solution.
I have a class using django.db as follows:
from django.db import models
class myClass(models.Model):
"My class"
class Meta:
db_table = 'my_table'
field1 = models.CharField()
# more fields here
def some_function(self):
return "hello"
I'd like to turn this into a generic class that does not have "my_table" hardcoded, so that I can use it in some way such as:
class GenericClass(models.Model):
class Meta:
db_table = None
# genericClass properties and methods here
class myClass(GenericClass):
id = models.CharField(max_length=20)
def __init__(self, *args, **kwargs):
super(self.__class__, self).Meta.db_table = 'my_table'
super(self.__class__, self).__init__(*args, **kwargs)
However, this gives me the following error (using Python 2.7 / Django 1.11):
Local field u'id' in class 'myClass' clashes with field of similar name from base class 'GenericClass'
Is there any way to create a generic class that extends django.db.models.Model, that supports a non-default database table name without hardcoding Meta.db_table?
I guess what you are asking is a Django equivalent of AbstractSuperClass / SuperClass.
If what you want is AbstractSuperClass for django model you can define it like
class AbstractModel(models.Model):
field1 = ...
field2 = ...
...
class Meta:
abstract = True
class SubClassModel(models.Model):
field3 = ...
class Meta:
db_table=sub_class_db_table
Your AbstractModel won't have a database table. It simply acts as a superclass to store common fields (and even you can define common functions).
If you DESC your sub_class_db_table, you will find field1, field2, field3.
See this for more details.
If you want to create a db_table hierarchy itself (field1 and field2 will be in the super_class_table and field3 will be in sub_class_table), you can use Multi-table inheritance. This will be useful especially if there is a service-contract scenario - A team or a module owns common functionality including the db_table, its maintenance. But this way is less commonly used.
I want to serialize a model, but want to include an additional field that requires doing some database lookups on the model instance to be serialized:
class FooSerializer(serializers.ModelSerializer):
my_field = ... # result of some database queries on the input Foo object
class Meta:
model = Foo
fields = ('id', 'name', 'myfield')
What is the right way to do this? I see that you can pass in extra "context" to the serializer, is the right answer to pass in the additional field in a context dictionary?
With that approach, the logic of getting the field I need would not be self-contained with the serializer definition, which is ideal since every serialized instance will need my_field. Elsewhere in the DRF serializers documentation it says "extra fields can correspond to any property or callable on the model". Are "extra fields" what I'm talking about?
Should I define a function in Foo's model definition that returns my_field value, and in the serializer I hook up my_field to that callable? What does that look like?
Happy to clarify the question if necessary.
I think SerializerMethodField is what you're looking for:
class FooSerializer(serializers.ModelSerializer):
my_field = serializers.SerializerMethodField('is_named_bar')
def is_named_bar(self, foo):
return foo.name == "bar"
class Meta:
model = Foo
fields = ('id', 'name', 'my_field')
http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
You can change your model method to property and use it in serializer with this approach.
class Foo(models.Model):
. . .
#property
def my_field(self):
return stuff
. . .
class FooSerializer(ModelSerializer):
my_field = serializers.ReadOnlyField(source='my_field')
class Meta:
model = Foo
fields = ('my_field',)
Edit: With recent versions of rest framework (I tried 3.3.3), you don't need to change to property. Model method will just work fine.
With the last version of Django Rest Framework, you need to create a method in your model with the name of the field you want to add. No need for #property and source='field' raise an error.
class Foo(models.Model):
. . .
def foo(self):
return 'stuff'
. . .
class FooSerializer(ModelSerializer):
foo = serializers.ReadOnlyField()
class Meta:
model = Foo
fields = ('foo',)
if you want read and write on your extra field, you can use a new custom serializer, that extends serializers.Serializer, and use it like this
class ExtraFieldSerializer(serializers.Serializer):
def to_representation(self, instance):
# this would have the same as body as in a SerializerMethodField
return 'my logic here'
def to_internal_value(self, data):
# This must return a dictionary that will be used to
# update the caller's validation data, i.e. if the result
# produced should just be set back into the field that this
# serializer is set to, return the following:
return {
self.field_name: 'Any python object made with data: %s' % data
}
class MyModelSerializer(serializers.ModelSerializer):
my_extra_field = ExtraFieldSerializer(source='*')
class Meta:
model = MyModel
fields = ['id', 'my_extra_field']
i use this in related nested fields with some custom logic
My response to a similar question (here) might be useful.
If you have a Model Method defined in the following way:
class MyModel(models.Model):
...
def model_method(self):
return "some_calculated_result"
You can add the result of calling said method to your serializer like so:
class MyModelSerializer(serializers.ModelSerializer):
model_method_field = serializers.CharField(source='model_method')
p.s. Since the custom field isn't really a field in your model, you'll usually want to make it read-only, like so:
class Meta:
model = MyModel
read_only_fields = (
'model_method_field',
)
If you want to add field dynamically for each object u can use to_represention.
class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
fields = ('id', 'name',)
def to_representation(self, instance):
representation = super().to_representation(instance)
if instance.name!='': #condition
representation['email']=instance.name+"#xyz.com"#adding key and value
representation['currency']=instance.task.profile.currency #adding key and value some other relation field
return representation
return representation
In this way you can add key and value for each obj dynamically
hope u like it
This worked for me.
If we want to just add an additional field in ModelSerializer, we can
do it like below, and also the field can be assigned some val after
some calculations of lookup. Or in some cases, if we want to send the
parameters in API response.
In model.py
class Foo(models.Model):
"""Model Foo"""
name = models.CharField(max_length=30, help_text="Customer Name")
In serializer.py
class FooSerializer(serializers.ModelSerializer):
retrieved_time = serializers.SerializerMethodField()
#classmethod
def get_retrieved_time(self, object):
"""getter method to add field retrieved_time"""
return None
class Meta:
model = Foo
fields = ('id', 'name', 'retrieved_time ')
Hope this could help someone.
class Demo(models.Model):
...
#property
def property_name(self):
...
If you want to use the same property name:
class DemoSerializer(serializers.ModelSerializer):
property_name = serializers.ReadOnlyField()
class Meta:
model = Product
fields = '__all__' # or you can choose your own fields
If you want to use different property name, just change this:
new_property_name = serializers.ReadOnlyField(source='property_name')
As Chemical Programer said in this comment, in latest DRF you can just do it like this:
class FooSerializer(serializers.ModelSerializer):
extra_field = serializers.SerializerMethodField()
def get_extra_field(self, foo_instance):
return foo_instance.a + foo_instance.b
class Meta:
model = Foo
fields = ('extra_field', ...)
DRF docs source
Even though, this is not what author has wanted, it still can be considered useful for people here:
If you are using .save() ModelSerializer's method, you can pass **kwargs into it. By this, you can save multiple dynamic values.
i.e. .save(**{'foo':'bar', 'lorem':'ipsum'})
Add the following in serializer class:
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['package_id'] = "custom value"
return representation
I need to make a form, which have 1 select and 1 text input. Select must be taken from database.
model looks like this:
class Province(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(max_length=30)
def __unicode__(self):
return self.name
It's rows to this are added only by admin, but all users can see it in forms.
I want to make a ModelForm from that. I made something like this:
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=CHOICES),
}
but it doesn't work. The select tag is not displayed in html. What did I wrong?
UPDATE:
This solution works as I wanto it to work:
class ProvinceForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ProvinceForm, self).__init__(*args, **kwargs)
user_provinces = UserProvince.objects.select_related().filter(user__exact=self.instance.id).values_list('province')
self.fields['name'].queryset = Province.objects.exclude(id__in=user_provinces).only('id', 'name')
name = forms.ModelChoiceField(queryset=None, empty_label=None)
class Meta:
model = Province
fields = ('name',)
Read Maersu's answer for the method that just "works".
If you want to customize, know that choices takes a list of tuples, ie (('val','display_val'), (...), ...)
Choices doc:
An iterable (e.g., a list or tuple) of
2-tuples to use as choices for this
field.
from django.forms.widgets import Select
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=( (x.id, x.name) for x in CHOICES )),
}
ModelForm covers all your needs (Also check the Conversion List)
Model:
class UserProvince(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
Form:
class ProvinceForm(ModelForm):
class Meta:
model = UserProvince
fields = ('province',)
View:
if request.POST:
form = ProvinceForm(request.POST)
if form.is_valid():
obj = form.save(commit=True)
obj.user = request.user
obj.save()
else:
form = ProvinceForm()
If you need to use a query for your choices then you'll need to overwrite the __init__ method of your form.
Your first guess would probably be to save it as a variable before your list of fields but you shouldn't do that since you want your queries to be updated every time the form is accessed. You see, once you run the server the choices are generated and won't change until your next server restart. This means your query will be executed only once and forever hold your peace.
# Don't do this
class MyForm(forms.Form):
# Making the query
MYQUERY = User.objects.values_list('id', 'last_name')
myfield = forms.ChoiceField(choices=(*MYQUERY,))
class Meta:
fields = ('myfield',)
The solution here is to make use of the __init__ method which is called on every form load. This way the result of your query will always be updated.
# Do this instead
class MyForm(forms.Form):
class Meta:
fields = ('myfield',)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make the query here
MYQUERY = User.objects.values_list('id', 'last_name')
self.fields['myfield'] = forms.ChoiceField(choices=(*MYQUERY,))
Querying your database can be heavy if you have a lot of users so in the future I suggest some caching might be useful.
the two solutions given by maersu and Yuji 'Tomita' Tomita perfectly works, but there are cases when one cannot use ModelForm (django3 link), ie the form needs sources from several models / is a subclass of a ModelForm class and one want to add an extra field with choices from another model, etc.
ChoiceField is to my point of view a more generic way to answer the need.
The example below provides two choice fields from two models and a blank choice for each :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=([['','-'*10]]+[[x.id, x.__str__()] for x in Speakers.objects.all()]))
event = forms.ChoiceField(choices=( [['','-'*10]]+[[x.id, x.__str__()] for x in Events.objects.all()]))
If one does not need a blank field, or one does not need to use a function for the choice label but the model fields or a property it can be a bit more elegant, as eugene suggested :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=((x.id, x.__str__()) for x in Speakers.objects.all()))
event = forms.ChoiceField(choices=(Events.objects.values_list('id', 'name')))
using values_list() and a blank field :
event = forms.ChoiceField(choices=([['','-------------']] + list(Events.objects.values_list('id', 'name'))))
as a subclass of a ModelForm, using the one of the robos85 question :
class MixedForm(ProvinceForm):
speaker = ...