So, you’re not going to like the answer, partly because I’m not yet done writing the code and partly because it’s a lot of work.
What you need to do, as I discovered when I ran into this myself, is:
- Spend a lot of time reading through the formset and model-formset code to get a feel for how it all works (not helped by the fact that some of the functionality lives on the formset classes, and some of it lives in factory functions which spit them out). You will need this knowledge in the later steps.
- Write your own formset class which subclasses from
BaseInlineFormSet
and acceptsinitial
. The really tricky bit here is that you must override__init__()
, and you must make sure that it calls up toBaseFormSet.__init__()
rather than using the direct parent or grandparent__init__()
(since those areBaseInlineFormSet
andBaseModelFormSet
, respectively, and neither of them can handle initial data). - Write your own subclass of the appropriate admin inline class (in my case it was
TabularInline
) and override itsget_formset
method to return the result ofinlineformset_factory()
using your custom formset class. - On the actual
ModelAdmin
subclass for the model with the inline, overrideadd_view
andchange_view
, and replicate most of the code, but with one big change: build the initial data your formset will need, and pass it to your custom formset (which will be returned by yourModelAdmin
‘sget_formsets()
method).
I’ve had a few productive chats with Brian and Joseph about improving this for future Django releases; at the moment, the way the model formsets work just make this more trouble than it’s usually worth, but with a bit of API cleanup I think it could be made extremely easy.