ModelFormsets in a Django is an advanced way of handling multiple forms created using a model and use them to create model instances. In other words, ModelFormsets are a group of forms in Django. One might want to initialize multiple forms on a single page all of which may involve multiple POST requests, for example
class GeeksModel(models.Model):
title = models.CharField(max_length = 200)
description = models.TextField()
Now if one wants to create a modelformset for this model, modelformset_factory needs to be used. A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid.
from django.forms import formset_factory
GeeksFormSet = modelformset_factory(GeeksModel)
Creating and using Django ModelFormsets
Illustration of Rendering Django ModelFormsets manually using an Example. Consider a project named geeksforgeeks
having an app named geeks
.
Refer to the following articles to check how to create a project and an app in Django.
In your geeks app make a new file called models.py where you would be making all your models. To create a Django model you need to use Django Models. Let’s demonstrate how,
In your models.py
Enter the following,
from django.db import models
class GeeksModel(models.Model):
title = models.CharField(max_length = 200 )
description = models.TextField()
def __str__( self ):
return self .title
|
Let’s explain what exactly is happening, left side denotes the name of the field and to right of it, you define various functionalities of an input field correspondingly. A field’s syntax is denoted as
Syntax :
Field_name = models.FieldType(attributes)
Now to create a simple formset of this form, move to views.py and create a formset_view
as below.
from django.shortcuts import render
from .forms import GeeksForm
from django.forms import formset_factory
def formset_view(request):
context = {}
GeeksFormSet = modelformset_factory(GeeksForm)
formset = GeeksFormSet()
context[ 'formset' ] = formset
return render(request, "home.html" , context)
|
To render the formset through HTML, create a html file “home.html”. Now let’s edit templates > home.html
< form method = "POST" enctype = "multipart/form-data" >
{% csrf_token %}
{{ formset.as_p }}
< input type = "submit" value = "Submit" >
</ form >
|
All set to check if our formset is working or not let’s visit http://localhost:8000/.

Our modelformset
is working completely. Let’s learn how to modify this formset to use extra features of this formset.
How to create Multiple forms using Django ModelFormsets
Django formsets are used to handle multiple instances of a form. One can create multiple forms easily using extra
attribute of Django Formsets. In geeks/views.py,
from django.shortcuts import render
from .models import GeeksModel
from django.forms import modelformset_factory
def modelformset_view(request):
context = {}
GeeksFormSet = modelformset_factory(GeeksModel, fields = [ 'title' , 'description' ], extra = 3 )
formset = GeeksFormSet()
context[ 'formset' ] = formset
return render(request, "home.html" , context)
|
The keyword argument extra
makes multiple copies of same form. If one wants to create 5 forms enter extra = 5
and similarly for others. Visit http://localhost:8000/ to check if 5 forms are created.

Handling Multiple forms using Django Formsets
Creating a form is much easier than handling the data entered into those fields at the back end. Let’s try to demonstrate how one can easily use the data of a model formset in a view. When trying to handle formset, Django formsets required one extra argument {{ formset.management_data }}. To know more about Management data, Understanding the ManagementForm.
In templates/home.html
,
< form method = "POST" enctype = "multipart/form-data" >
{{ formset.management_data }}
{% csrf_token %}
{{ formset.as_p }}
< input type = "submit" value = "Submit" >
</ form >
|
Now to check how and what type of data is being rendered edit formset_view to print the data. In geeks/view.py
,
from django.shortcuts import render
from .forms import GeeksForm
from django.forms import formset_factory
def formset_view(request):
context = {}
GeeksFormSet = formset_factory(GeeksForm, extra = 3 )
formset = GeeksFormSet(request.POST or None )
if formset.is_valid():
for form in formset:
print (form.cleaned_data)
context[ 'formset' ] = formset
return render(request, "home.html" , context)
|
Now let’s try to enter data in the formset through http://localhost:8000/

Hit submit and data will be saved in GeeksModel where server is running. One can use this data in any manner conveniently now.

Similar Reads
Django Form
When one creates a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks. This article revolves around various fields one can use in a form along with various features and techniques concerned with Django Forms. D
6 min read
How to create a form using Django Forms ?
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model
3 min read
Django ModelFormSets
ModelFormsets in a Django is an advanced way of handling multiple forms created using a model and use them to create model instances. In other words, ModelFormsets are a group of forms in Django. One might want to initialize multiple forms on a single page all of which may involve multiple POST requ
4 min read
Django ModelForm
Django ModelForm is a class that is used to directly convert a model into a Django form. If youâre building a database-driven app, chances are youâll have forms that map closely to Django models. For example, a User Registration model and form would have the same quality and quantity of model fields
4 min read
Render Django Forms as table
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as paragraph
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render HTML Forms (GET & POST)
Django is often called "Batteries Included Framework" because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a form is a collection of elements inside <form>...</form> that allow a visitor to do thing
5 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Initial form data _ Django Forms
After creating a Django Form, if one requires some or all fields of the form be filled with some initial data, one can use functionality of Django forms to do so. It is not the same as a placeholder, but this data will be passed into the view when submitted. There are multiple methods to do this, mo
3 min read