How can I display multiple django modelformset forms together?

Posted by JT on Stack Overflow See other posts from Stack Overflow or by JT
Published on 2010-06-09T23:50:03Z Indexed on 2010/06/09 23:52 UTC
Read the original article Hit count: 302

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

for base_form, likes_form in map(None, base_forms, likes_forms):

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about django-models