There are a couple things I’ve found to be helpful to keep in mind when working with formsets.
-
A formset can be thought of as a “list of forms” that can be worked on as a group. So if you would want to create “n” number of instances of a particular form and put them in a list - that’s a formset.
-
A formset_factory creates a formset Class - not an actual formset. You create the formset by making an instance of that class. It’s similar in principle to defining a form. You create a form class in your forms.py, then create the instances of that form in your views. The difference is you don’t defined a formset as
class MyFormset
, you define it asMyFormset = formset_factory(...)
. You still need to create the formset to be used. (e.g. my_formset = MyFormset(…)`) -
The formset is “lazy”. The instances of the individual forms are not created when the formset is create. Those forms aren’t created until the first time they’re “needed”. This means you have the ability to alter that formset after being created but before being used.
Personally, I found reading the source code for formsets to be very helpful with understanding how they work.