hi.

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:
myinlineformsetname_set-TOTAL_FORMS
myinlineformsetname_set-INITIAL_FORMS

2. each new form must have its fields named according to the following
pattern:
myinlineformsetname_set-<index>-<fieldname>

like:

myinlineformsetname_set-0-name
myinlineformsetname_set-0-description
myinlineformsetname_set-0-age

myinlineformsetname_set-1-name
myinlineformsetname_set-1-description
myinlineformsetname_set-1-age

3. each form must also contain hidden fields holding the id's of the
form and of its parent, like:

myinlineformsetname_set-0-parent
myinlineformsetname_set-0-id
myinlineformsetname_set-1-parent
myinlineformsetname_set-1-id

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
(inline forms).

A) what should I echo on that myinlineformsetname_set-INITIAL_FORMS
field? should it be populated with the amount of forms initially on
screen?
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
sure.

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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to