Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formsets made from different type of forms

Tags:

django

Is it possible to have a formset in Django made up of heterogeneous forms considering that the formset_factory() function takes only one form class as parameter? Something like:

    formset_factory(FormType1, FormType2,...)
like image 814
Al Bundy Avatar asked Mar 04 '26 00:03

Al Bundy


1 Answers

There's nothing that fundamentally prevents the concept of formsets from working with multiple form classes, but the factories haven't been designed with that in mind. To achieve it you'd realistically have to write your own formset factory that overrides a lot of the existing API.

Consider this. You want a formset consisting on two different form classes. When the formset is instantiated, it needs to create min(current + extra, max_forms) forms. But how will it know which form class to create? It's easy if all the forms are the same, but for multiple classes you have to figure out a way of making this choice. I'm sure you can imagine how this will get rather complicated. The form wizards functionality might provide some possible ideas for how such an API could work, but it's a long shot,

I think finding an alternative approach might be the best choice in this case.

like image 191
Andrew Ingram Avatar answered Mar 06 '26 15:03

Andrew Ingram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!