Formsets & Inline Formsets

A formset is a layer that manages several copies of the same form on one page, validating them as a group, so a user can add or edit many rows at once instead of submitting one form at a time.

Learn Formsets & Inline Formsets in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll build a plain formset with formset_factory, understand the management form that tracks how many forms were submitted, and use modelformset_factory and inlineformset_factory to edit model rows and related child objects.

A single Django form handles one record. A formset wraps a form class so you can render and validate many copies at once. You create the formset class with formset_factory , telling it the form and how many extra blank rows to show. When the page is submitted, the formset validates every form and is only valid if all of them are valid.

A formset needs to know how many forms were submitted . It tracks this with a hidden management form — a few fields named TOTAL_FORMS , INITIAL_FORMS , MIN_NUM_FORMS , and MAX_NUM_FORMS . You must render {'{ }'} in your template, or the submitted data won't include these counters and Django will raise a validation error.

The two model-aware variants save you from writing forms by hand. modelformset_factory builds a formset bound to one model, so you can edit a grid of rows from that table. inlineformset_factory is for editing the children of a parent through a foreign key — like all the Book rows that belong to one Author — and it sets each child's foreign key to the parent automatically.

A formset is valid only when every form is valid. Replace the blank so is_valid is True only when no form has errors.

❌ "ManagementForm data is missing or has been tampered with"

You rendered the forms but forgot the hidden management form.

❌ My inline formset rows aren't linked to the parent

You didn't pass the parent instance, so the foreign key was never set.

✅ Fix: construct the inline formset with instance=parent .

❌ Deleting a row in the form doesn't actually delete it

The DELETE field only appears when you opt in.

✅ Fix: pass can_delete=True to the factory and call formset.save() .

Write a factory that returns a validator for a list of forms, then use it to validate a list of Product dicts (name required, price positive) and report a summary.

Lesson complete — you can edit many records at once!

You built a formset with formset_factory, learned why the management form is required, and used modelformset_factory and inlineformset_factory to edit model rows and the related children of a parent — including deletions.

🚀 Up next: Class-Based View Mixins — compose reusable behavior into your views with LoginRequiredMixin and friends.

Practice quiz

What does a formset manage?

  • A single form's fields
  • Several copies of the same form on one page
  • Database migrations
  • URL routing

Answer: Several copies of the same form on one page. A formset manages multiple copies of the same form so users can fill many rows at once.

Which factory builds a basic formset from a form class?

  • modelformset_factory
  • inlineformset_factory
  • form_collection
  • formset_factory

Answer: formset_factory. formset_factory takes the form class and options like the number of extra blank forms.

Which factory option controls how many blank forms are shown?

  • blank
  • extra
  • spare
  • rows

Answer: extra. The 'extra' argument sets how many empty forms appear.

What must you render in the template to avoid a ManagementForm error?

  • {{ formset.errors }}
  • {{ formset.media }}
  • {{ formset.management_form }}
  • {{ formset.as_table }}

Answer: {{ formset.management_form }}. The management form holds TOTAL_FORMS and INITIAL_FORMS counters Django needs.

Which hidden field reports how many forms were submitted?

  • TOTAL_FORMS
  • FORM_COUNT
  • NUM_FORMS
  • SUBMITTED

Answer: TOTAL_FORMS. TOTAL_FORMS, in the management form, tells Django how many forms came back.

Which factory edits many rows of a single model?

  • formset_factory
  • modelformset_factory
  • inlineformset_factory
  • modelform_factory

Answer: modelformset_factory. modelformset_factory creates a formset bound to one model for editing many rows.

Which factory edits child objects related to one parent by foreign key?

  • formset_factory
  • modelformset_factory
  • inlineformset_factory
  • parent_factory

Answer: inlineformset_factory. inlineformset_factory edits child rows tied to a parent and sets the foreign key for you.

What does inlineformset_factory do with the foreign key to the parent?

  • Ignores it
  • Requires you to set it manually
  • Deletes it
  • Sets it automatically on each child

Answer: Sets it automatically on each child. The inline formset automatically links each child to the parent and sets the FK.

How do you let users delete rows in a formset?

  • Pass can_delete=True to the factory
  • Override the delete() method
  • Set extra=0
  • Add a delete button manually only

Answer: Pass can_delete=True to the factory. can_delete=True adds a DELETE checkbox; checked forms are removed on save.

Which option adds an ORDER field so users can reorder rows?

  • can_sort=True
  • can_order=True
  • ordered=True
  • sortable=True

Answer: can_order=True. can_order=True adds an ORDER field for reordering rows.