Imagine having a simple model like the one bellow:
from utils.validators import name_validator
class Customer(models.Model):
name = models.CharField(verbose_name="Customer Name", validators=[name_validator])
email = models.EmailField(verbose_name="Customer Email")
def __str__(self):
return self.name
Now if I explicitly define a filed on my serializer, both validators and verbose_name are lost. I can use label= and validatos= when defining the field on my serializer but I don't want to repeat myself. What if I have multiple serializer pointing to the same Model?
class CustomerSerilizer(serializers.ModelSerializer):
custom_field_name = serializers.CharField(source="name")
class Meta:
model = Customer
fields = "__all__"
Is there anyway to prevent this from happening?
I'm not sure if it's the perfect way of doing this or not, but I managed to achieve my desired behavior by writing a custom ModelSerializer which sets label and validators if they are not being passed when explicitly defining a field on the serializer.
class CustomModelSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(CustomModelSerializer, self).__init__(*args, **kwargs)
model = self.Meta.model
model_fields = [f.name for f in model._meta.get_fields()]
for field_name, field_instance in self.fields.items():
source_field = field_instance.source
if source_field in model_fields:
model_field = model._meta.get_field(source_field)
if "label" not in field_instance._kwargs:
field_instance.label = model_field.verbose_name
if "validators" not in field_instance._kwargs:
field_instance.validators.extend(model_field.validators)
I am struggling to create my custom generic view in django to easily create search pages for certain models. I'd like to use it like this:
class MyModelSearchView(SearchView):
template_name = 'path/to/template.html'
model = MyModel
fields = ['name', 'email', 'whatever']
which will result in a view that returns a search form on GET and both form and results on POST.
The fields specifies which fields of MyModel will be available for a user to search.
class SearchView(FormView):
def get_form(self, form_class=None):
# what I'v already tried:
class SearchForm(ModelForm):
class Meta:
model = self.model
fields = self.fields
return SearchForm()
def post(self, request, *args, **kwargs):
# perform searching and return results
The problem with the code above is that form will not be submitted if certain fields are not be properly filled. User should be allowed to provide only part of fields to search but with the code I provided the form generated with ModelForm prevents that (for example because a field in a model cannot be blank).
My questions are:
Is it possible to generate a form based on a model to omit this behaviour?
Or is there any simpler way to create SearchView class?
I don't want to manually write forms if it's possible.
One way to accomplish this is to set blank=True on the field in MyModel, as indicated in the docs:
If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.
But for this to be a generic solution, you can't count on being able to modify the model fields. You can instead set the fields' required attribute to False immediately after the instance is created:
class SearchForm(ModelForm):
class Meta:
model = self.model
fields = self.fields
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for (field_name, field) in self.fields.items():
field.required = False
Since you're using the ModelForm for searching, you should set all the fields as required=False, by overriding the __init__ method:
def get_form(self, form_class=None):
# what I'v already tried:
class SearchForm(ModelForm):
class Meta:
model = self.model
fields = self.fields
def __init__(self, *args, **kwargs):
super(SearchForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].required = False
return SearchForm()
Though I suggest you should user django-filter, which makes it easier and cleaner to filter your searches. First you need to install it:
pip install django-filter
Then add it to your INSTALLED_APPS. After that you can create a filters.py file in your app:
# myapp/filters.py
import django_filters as filters
from .models import MyModel
MyModelFilterSet(filters.FilterSet):
class Meta:
model = MyModel
fields = ['name', 'email', 'whatever']
By default it's going to filter with the __exact lookup. You can change this in a couple of ways, just take a look here and here. To know which lookups you can use, take a look here.
After creating your filters.py file you can add it to a View, like a ListView:
# myapp/views.py
from django.views.generic import ListView
from .filters import MyModelFilterSet
from .models import MyModel
class MyModelSearchView(ListView):
template_name = 'path/to/template.html'
model = MyModel
def get_queryset(self):
qs = self.model.objects.all()
filtered_model_list = MyModelFilterSet(self.request.GET, queryset=qs)
return filtered_model_list.qs
There's a lot more you can do with django-filter. Here's the full documentation.
I'm using a modelformset_factory to edit multiple instances of Product in the same form:
ProductFormSet = modelformset_factory(Product, fields=('code', 'state'))
form_products = ProductFormSet()
It works well.
But now I need to display an additional field of the Product model in the form but only for a specific instance of Product. I'm not sure if it can be done in a simple manner in Django. Is it possible to do so using a modelformset_factory?
You can specify the form in the modelformset_factory, so create a model form (in forms.py if you have one) override the __init__method to add extra fields.
I would move the fields from the formsetfactory arguments to the form
in forms.py (assuming you have one)
class ProductForm(forms.ModelForm):
model = Product
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs :
product = kwargs['instance']
# to add an extra field, add something like this
self.fields['extra_field'] = forms.CharField(max_length=30)
class Meta:
fields = ('code', 'state')
Then pass that to your modelformset factory with the form argument
ProductFormSet = modelformset_factory(Product, form=ProductForm )
form_products = ProductFormSet()
I am trying to set the fieldorder of my form. but somehow it just stays in alphabetical order. Anyone has some suggestions? i tried class Meta: fields = ["field", "field"] and adding a keyOrder in the init
form:
class HangarFilterForm(forms.Form):
FIELDS = [
("", ""),
("warp", "Warp"),
("cargo_space", "Cargo Space"),
("smuggle_bay", "Smuggle Bay"),
("dock", "Dock/Undock"),
("enter_warp", "Enter Warp"),
("fuel_bay", "Fuel Bay"),
("fuel_cost", "Fuel Cost"),
]
PER_PAGE = [
(10, ""),
(5, "5 ships"),
(10, "10 ships"),
(25, "25 ships"),
(50, "50 ships"),
]
field_1 = forms.ChoiceField(choices=FIELDS, label="1st attribute", required=False)
field_2 = forms.ChoiceField(choices=FIELDS, label="2nd attribute", required=False)
per_page = forms.ChoiceField(choices=PER_PAGE, required=False)
def __init__(self, *args, **kwargs):
super(HangarFilterForm, self).__init__(*args, **kwargs)
self.fields['planet'] = forms.ChoiceField(
choices=[("", "")] + [ (o.id, o.name) for o in lanet.objects.all().order_by("name")],
required=False)
self.fields['type'] = forms.ChoiceField(
choices=[("", "")] + [ (o[0], o[1]) for o in ShipTemplate.SHIP_TYPES], required=False)
self.fields.keyOrder = ["planet", "type", "field_1", "field_2", "per_page"]
In Django 1.9, new way of forcing the order of form's fields has been added : field_order.
Take a look (link to version 1.9):
https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.field_order
(and a link to dev version):
https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.field_order
Find below a short example (using Django 1.9)
models.py:
from django.db import models
class Project(models.Model):
end_date = models.DateField(verbose_name='End date',
blank=True)
start_date = models.DateField(verbose_name='Start date',
blank=True)
title = models.CharField(max_length=255,
blank=False,
verbose_name='Title')
def __str__(self):
return self.title
forms.py
from django.forms import ModelForm, DateTimeField, SelectDateWidget
from XXX.models import Project
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = '__all__'
start_date = DateTimeField(widget=SelectDateWidget)
end_date = DateTimeField(widget=SelectDateWidget)
field_order = ['start_date', 'end_date']
In this example the fields will be rearranged to:
start_date <== using the list in the form class
end_date <== using the list in the form class
title <== not mentioned in the list, thus using the default ordering
I tried setting fields in Meta part of form in django 2.1 and it also did the trick:
class MyForm(forms.ModelForm):
...
class Meta:
model = Contact
fields = ('field_1', 'field_2', 'field_3', 'field_4',)
This is some code that I've done in the past to rearrange the field order in forms that has worked; you could probably put this into a mixin for use elsewhere. Let me know how it goes.
from django.utils.datastructures import SortedDict
class HangarFilterForm(forms.Form):
ordered_field_names = ['planet', 'type', 'field_1', 'field_2', 'per_page']
def __init__(self, *args, **kwargs):
super(HangarFilterForm, self).__init__(*args, **kwargs)
# Your field initialisation code
self.rearrange_field_order()
def rearrange_field_order(self):
original_fields = self.fields
new_fields = SortedDict()
for field_name in self.ordered_field_names:
field = original_fields.get(field_name)
if field:
new_fields[field_name] = field
self.fields = new_fields
If you want to keep track of the original file order for some reason, you can just change original_fields to self.original_fields in rearrange_field_order.
Might be a little bit off topic. Using django crispy forms and their Layout objects can help a great deal with formatting forms the way you exactly want. Which includes rearranging the field order.
A sample to illustrate:
class UpdateUserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('email'),
Field('quote'),
Field('website', placeholder="http://"),
Field('logo', template="user/logoInput.html"),
HTML('<label class="control-label">Other settings</label>'),
Field('emailVisible'),
Field('subscribeToEmails'),
Field('mpEmailNotif'),
Field('showSmileys'),
Field('fullscreen'),
)
class Meta:
model = ForumUser
fields = ('email', 'emailVisible', 'subscribeToEmails', 'mpEmailNotif',
'logo', 'quote', 'website', 'showSmileys', 'fullscreen')
I used the bspink's way with some improvements. You don't need to define the fields that you don't want to change their ordering. Define only what you want to put to the top as ordered in themself.
(Be sure you are using the Python 3.7+)
class SomeForm(forms.Form):
# define only you want to put top, no need to define all of them
# unless you need more specific ordering
ordered_field_names = ['planet', 'type']
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
# call initialization code
self.rearrange_field_order()
def rearrange_field_order(self):
# add defined fields first
new_fields = {field_name: self.fields.get(field_name) for field_name in self.ordered_field_names}
# then add others whose not defined in order list
for key, value in self.fields.items():
if key not in new_fields:
new_fields[key] = value
self.fields = new_fields
I have a modelform and im creating additional fields (that do not exist in model) for its form.
I know you can reorder the fields in modelform like it says in the docs.
But the problem is - i want the additional fields to be rendered BEFORE the other fields.
Is it possible to somehow reorder the fields of the form before rendering? How does form object keep track of the order of its fields anyway?
Alan
No matter. It seems i found answer already and this seems to do the trick, since i have added 2 additional fields:
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.build_fields()
l = len(self.fields.keyOrder)
for i in range(0,2):
f = self.fields.keyOrder[l-1]
self.fields.keyOrder.pop(l-1)
self.fields.keyOrder.insert(0, f)
This above was my initial fix. Later on i found out that it did not cut any more. Then i did this :
class AlertForm(forms.ModelForm):
class Meta:
model = Message
fields = model_fields
def __init__(self, *args, **kwargs):
super(AlertForm, self).__init__(*args, **kwargs)
self.build_fields()
newKeyOrder = []
newKeyOrder.append('field_that_had_to_be_first')
if typechange:
newKeyOrder.append('field_thats_sometimes_necessary')
newKeyOrder += model_fields
self.fields.keyOrder = newKeyOrder
The solutions above no longer works with django 2 (I don't know since when)...
But now, there's an ordered dict fields property on ModelForm that we can use to reorder the fields...
class MyForm(forms.ModelForm):
class Meta:
fields = ['model_field1', 'model_field2']
model = MyModel
extra_field = forms.CharField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for k in self._meta.fields:
self.fields.move_to_end(k)
The fields attribute of your ModelForm`s Meta class define which fields to show and in which order.
Use the fields attribute of the ModelForm's inner Meta class. This attribute, if given, should be a list of field names to include in the form. The order in which the fields names are specified in that list is respected when the form renders them.