VOOZH about

URL: https://www.geeksforgeeks.org/python/django-modelformsets/

⇱ Django ModelFormSets - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Django ModelFormSets

Last Updated : 2 Dec, 2025

Django ModelFormsets allow managing multiple model-based forms on a single page. They enable creating, updating, or deleting multiple instances of a model at once, using a group of forms tied to the model's fields. Each form in the formset corresponds to a single model instance.

Using ModelFormsets

Consider a Django project with an app containing the GeeksModel model.

In models.py:

  • title and description are the fields representing model data.
  • __str__ method helps display the model instances by their title.

Creating a ModelFormset

To create a formset for this model, use Django’s modelformset_factory function.

This creates a formset class that generates forms based on the GeeksModel.

ModelFormsets in Views

In views.py, create a view to render and process the formset:

  • extra=3 argument means three empty forms will be displayed for adding new entries.
  • On POST, the formset is validated and saved automatically if valid.

Rendering ModelFormsets in Templates

In home.html, render the formset:

  • management_form is required to keep track of formset data and validate the forms correctly.
  • formset.as_p renders each form’s fields wrapped in <p> tags.

Test the Formset

Run the development server:

python manage.py runserver

Visit the development server: http://localhost:8000/:

👁 django-modelformsets
Model Form

Submitting the form saves data into GeeksModel, which can then be used throughout the application.

👁 django-model-formset-result
Django Formset
Comment
Article Tags: