Multiple forms and inheritance into single form

I have two forms FormA and FormB , i want to merge them into a single form. Ive tried using class mergedform (FormA, FormB) it doesnt work to bring both forms into the merged form class . The reason im doing this is because i need to parameterise calling a single form → mergedform. So i cant have 2 forms in my view and templates , i just want one form that is merged forms . Can i do this in django have one form that brings 2 different forms that are respectively attached to different models

First, are you talking about two model forms or two regular forms? (Or are you talking about one regular form and one model form?)

In general, I’m willing to guarantee that you can more easily work with two forms in your view and template than any of your other options, especially if you are using those forms independently.

If you’re talking about two regular form, you could create a new form with all the fields from both.

If you’re talking about one model form and one regular form, you could just add the fields from the other form to your model form.

But in either of those cases, it’ll actually be easier to work with two forms.

If you’re talking about two model forms, then using two forms is going to be your best option.


2 Model Forms with different Models. The business case is that we are using one code base for all our clients on the cloud . So some clients require for the same view say FormA & FormB and some FormA, some FormA& FormC. We have tried parameterised this into another table as a single form, the code looks neat and manageable. The alternative using 2 forms even 3 can be done, but as you can imagine we now have to hard code client name for different Form configuration and laced with a lof of if then else. The templates and post doesnt seem as messy

Anyway thats the use case i can do it in fields but again its just messy and a lot of codes. thanks for the response but i gather we cant merge 2 model forms to act as one connected to different models

Are you rendering these forms individually by field, or are you rendering them as {{ form.as_p }} (or something analogous to that)?

If the latter, it’s a lot easier than you’re making it sound.

Instead of building your views around an individual form, build your view around the concept of a “form_list”. Instantiate, render, and process these forms in each case by iterating through the list.

Yes, you’re not going to be able to directly use the Django-provided CBVs, but you could create your own views based on them.

I don’t think it’s going to be as messy as you may think it might be.

Be aware that a Form is not a “simple” Python class, and ModelForms even less so. There’s a lot of metaprogramming going on in the construction of the fundamental Django classes, and so they don’t work in the same way as a simpler class structure might. So no, you can’t just merge two model forms to act as one.

im using crispy forms so its just crispy FormA. Not thought of form_list will give it a shot and see if can cater for those various permutations of forms. I want one view for all clients.

Thanks again will try it and if it works will post back, cheers