Introduction
Django ModelFormsets is an advanced way of handling multiple forms created using a Django model and using them to create Django model instances. In other words, Django ModelFormsets are a group of multiple forms in Django. We might want to initiate multiple forms on a single web page, all or some of which may involve multiple POST requests. For example
class Coding Ninjas StudioModel(models.Model):
title = models.CharField(max_length = 200)
description = models.TextField()
If we want to create a Django ModelFormSet for this model, modelformset_factory() needs to be used. A Django formset is a layer of abstraction that works with multiple forms on the same page. It is best compared to a data grid.
from django.forms import formset_factory
Coding Ninjas StudioFormSet = modelformset_factory(Coding Ninjas StudioModel)Django Modelformsets factory
The Django modelformset_factory() method is the centrepiece of working with Django ModelFormSets. The modelformset_factory method may have nineteen arguments, nine of which are identical to standard Django FormSets, and the remaining ones are specific to Django ModelFormSets. The following snippet shows all the names and default values for each argument in the modelormset_factory method:
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)
In the above snippet, we can see that modelformset_factory() takes a lot of arguments. Let's see them in detail.
Argument |
Description |
| model | It is used to define the model class on which we need to create a ModelFormSet. |
| queryset | Define the queryset to create a formset. model.objects.all() as default value. |
| fields | Define the ModelForm fields to include as a part of a model to create the Django ModelFormSets. |
| exclude | Define the ModelForm fields to omit as a part of a model to create the Django ModelFormSets. |
| widgets | Define overriding widgets for a model form to create the ModelFormset. |
| localize_fields | Define ModelForm fields to localize (i.e., to support multiple languages) to create the ModelFormset. |
| labels | Define overriding the label for a model form to create the ModelFormSet. |
| help_text | Defines overriding help text for a model form to create the ModelFormSet. |
| error_messages | Define overriding error messages for a model form to create the ModelFormSet. |
| field_classes | Defines overriding field classes for a model form to create the ModelFormSet. |
| formsetfield_callback | Define a method to execute before creating a form field from the model field. |
Although the only required argument for Django ModelFormSets is the model field in the modelformset_factory method. This field defines the Django model class on which we will create Django ModelFormSets.
Read this, Introduction to JQuery






