How can I display multiple django modelformset forms in a grouped fieldsets?
- by JT
I have a problem with needing to provide multiple model backed forms on the same page. I understand how to do this with single forms, i.e. just create both the forms call them something different then use the appropriate names in the template.
Now how exactly do you expand that solution to work with modelformsets? The wrinkle, of course, is that each 'form' must be rendered together in the appropriate fieldset.
For example I want my template to produce something like this:
<fieldset>
<label for="id_base-0-desc">Home Base Description:</label>
<input id="id_base-0-desc" type="text" name="base-0-desc" maxlength="100" />
<label for="id_likes-0-icecream">Want ice cream?</label>
<input type="checkbox" name="likes-0-icecream" id="id_likes-0-icecream" />
</fieldset>
<fieldset>
<label for="id_base-1-desc">Home Base Description:</label>
<input id="id_base-1-desc" type="text" name="base-1-desc" maxlength="100" />
<label for="id_likes-1-icecream">Want ice cream?</label>
<input type="checkbox" name="likes-1-icecream" id="id_likes-1-icecream" />
</fieldset>
I am using a loop like this to process the results (after form validation)
base_models = base_formset.save(commit=False)
like_models = like_formset.save(commit=False)
for base_model, likes_model in map(None, base_models, likes_models):
which works as I'd expect (I'm using map because the # of forms can be different). The problem is that I can't figure out a way to do the same thing with the templating engine. The system does work if I layout all the base models together then all the likes models after wards, but it doesn't meet the layout requirements.
EDIT: Updated the problem statement to be more clear about what exactly I'm processing (I'm processing models not forms in the for loop)