I’d like to use the following form class in a modelformset. It takes a maps parameter and customizes the form fields accordingly.
class MyModelForm(forms.ModelForm):
def __init__(self, maps, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
#customize fields here
class Meta:
model = MyModel
My question is, how do I use this form in a modelformset? When I pass it using the form parameter like below, I get an exception.
MyFormSet = modelformset_factory(MyModel, form=MyModelForm(maps))
I suspect it wants the form class only, if so how do I pass the maps parameter to the form?
Keep in mind that Django uses class definition as a sort of DSL to define various things. As such, instantiating at places where it expects the class object will break things.
One approach is to create your own form factory. Something like:
Then you can do: