I'm working with inline formsets, and I plan to add them dynamically
in my application.
I did some tests and I found the following key points to handle:
1. my html must contain two hidden fields, named:
2. each new form must have its fields named according to the following
3. each form must also contain hidden fields holding the id's of the
form and of its parent, like:
these fields will have empty values if they represent new entities (if
the parent is not created yet, the -parent value is empty, and if the
form is new, the -id value is empty)
4. each form may optionally have a delete field like
myinlineformsetname_set-0-DELETE that will tell django to remove or
ignore that form (remove in case the form is bound to an existing
entity, or ignore if the form is related to a new entity, shown on
screen but that the user does not want to persist).
but I have two questions:
I have an entity X (parent, main form) and entities Y linked to X
A) what should I echo on that myinlineformsetname_set-INITIAL_FORMS
field? should it be populated with the amount of forms initially on
then I can see two scenarios:
A1. I'm creating my first Y form. in this case I'll show only one form
on screen, with 0 indexes and use INITIAL_FORMS = 0 or 1, I'm not
A2. I already have some Y on the database, let's say 3. then, I'll
show my html with two formsets (indexes 0, 1 and 2) and will set
INITIAL_FORMS to 3. the next time I press my add button, I'll add a
fourth form (index 3) empty and change my INITIAL_FORMS to 4. (should
I change it???)
I know that I must update the TOTAL_FORM always.
the key here is to know what exactly is that INITIAL_FORMS field for?
how show I use it?
B) easy one, just to confirm: whenever I want to remove a form from my
screen, I must not remove it from my POST, but instead hide it on
screen and CHECK its corresponding DELETE field, right? then, when
Django gets my post, he will know when to remove existing entities or
just ignore the new ones, right?
I did some tests and this approach seems to work. the key is to not
break the indexes sequence, but just hide the form on screen. I just
added this question to try to make this "how to" thread complete. :P
thanks in advance,
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to firstname.lastname@example.org.
To unsubscribe from this group, send email to email@example.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.