Django model formsets
Just as standard Django forms can be grouped together as a set in what's called a Django formset, Django model forms can also be grouped into what's called a model formset. Similarly, just as Django model forms resemble standard Django forms, model formsets also have a lot of similarities with standard formsets.
Up next, I'll describe the particularities of model formsets building on the knowledge presented in the last part of Chapter 6 on standard formsets. So if you're unfamiliar with formset terms (e.g. factory and management form), go back an read this last section, as the following assumes you have prior knowledge on these basic formset concepts.
Model formset factory
The
modelformset_factory()
method is the centerpiece to
working with model formsets. The
modelformset_factory()
method can accept up to
nineteen arguments, nine of which are identical to standard
formsets and the remaining ones specific to model formsets. The
following snippet illustrates all the names and default values for
each argument in the modelormset_factory()
method,
with bolded text representing model formset specific options.
modelformset_factory(model, queryset=model.objects.all(), form=ModelForm,fields=None, exclude=None, formset=BaseModelFormSet, extra=1, can_order=False, can_delete=False, max_num=None, min_num=None, validate_max=False, validate_min=False, widgets=None, localized_fields=None,labels=None, help_texts=None, error_messages=None, field_classes=None,formfield_callback=None)
As you can confirm in this
snippet, the only required argument (i.e. that doesn't have a
default value) for the modelformset_factory()
method
is model
. The meaning for each argument is the
following:
model
.- Defines the model class on which to create a formset.queryset
.- Defines the queryset to create a formset. By default, all model records are used to create the formset (i.e. themodel.objects.all()
queryset if used).fields
.- Defines the model form fields to include as part of the model to create the model formset -- just like thefields
meta model form option.exclude
.- Defines the model form fields to omit as part of the model to create the model formset -- just like theexclude
meta model form option.widgets
.- Defines overriding widgets for the model form to create the model formset -- just like thewidgets
meta model form option.localize_fields
.- Defines model form fields to localize (i.e. support multiple languages) to create the formset -- just like thelocalize_fields
meta model form option.labels
.- Defines overriding label for the model form to create the model formset -- just like thelabels
meta model form option.help_text
.- Defines overriding help text for the model form to create the model formset -- just like thehelp_texts
meta model form option.error_messages
.- Defines overriding error messages for the model form to create the model formset -- just like thehelp_texts
meta model form option.field_classes
.- Defines overriding field classes for the model form to create the model formset -- just like thefield_classes
meta model form option.-
formsetfield_callback
.- Defines a method to execute prior to creating a form field from a model field. Generally used to customize a model form field -- as described in the earlier model form section -- in the context of a formset.
Tip See Chapter 6 for details on the additional modelformset_factory options, which were already described in the standard formsets section.
Given that model formset logic is
a combination of the model form techniques described earlier and
formset techniques described in Chapter 6, I won't readdress the
same techniques once again. You can look over the book's source
code for a working model formset example in the source code for
Chapter 9 under the online
app.