Dynamically add a line to an inlineformset

25 views
Skip to first unread message

Andew Gee

unread,
Aug 20, 2009, 1:27:49 AM8/20/09
to Django users
Does anyone know how to add a new line to anlineformset dynamically? I
have a page that contains an inlineformset and I need to be able to
click a button and add a new line which will then be saved when the
form is submitted, is that possible?

Thanks
Andrew

Matthias Kestenholz

unread,
Aug 20, 2009, 4:09:05 AM8/20/09
to django...@googlegroups.com

Sure it is. Clone the HTML of the last line (or any empty line),
modifiy the id and name attributes (increment the counters by one) and
increment the TOTAL-FORMS hidden input field. There is no builtin
support for doing that in Django, because it is javascript framework
agnostic, but it's quite easy to do it anyway.

Maybe you find some inspiration how to do it in the admin-ui GSoC
branch? There, jQuery is used to provide exactly what you want.

Matthias


--
FeinCMS Django CMS building toolkit: http://spinlock.ch/pub/feincms/

Noah

unread,
Aug 20, 2009, 11:07:14 AM8/20/09
to Django users
http://www.djangosnippets.org/snippets/1389/

On Aug 20, 1:09 am, Matthias Kestenholz
<matthias.kestenh...@gmail.com> wrote:

Andew Gee

unread,
Aug 22, 2009, 1:53:13 AM8/22/09
to Django users
Thank you for your help.

I have managed to get the inlineforms to reproduce on my page, however
when I submit the form the extra inlines are ignored. I have printed
the formset when it hits the view and the new lines that I have added
have no values in them, the values that I enter in are not being
passed back by the request. Is this something that someone has seen
before?

Andrew

Matthias Kestenholz

unread,
Aug 22, 2009, 4:13:56 AM8/22/09
to django...@googlegroups.com
On Sat, Aug 22, 2009 at 7:53 AM, Andew Gee<gee.w...@gmail.com> wrote:
>
> Thank you for your help.
>
> I have managed to get the inlineforms to reproduce on my page, however
> when I submit the form the extra inlines are ignored. I have printed
> the formset when it hits the view and the new lines that I have added
> have no values in them, the values that I enter in are not being
> passed back by the request. Is this something that someone has seen
> before?
>

Yes, I've seen. It either means you are not correctly incrementing
TOTAL-FORMS (and therefore the formset code won't look at the added
parameters) or -- less probable -- you are not altering the name and
id attributes when creating new form rows. The second point is less
probable because you said that you can see the new fields on the
server side, even though you only see them as empty.

Another thing to check would be whether the said input fields exist
twice inside your form, and the later overwrite the former when the
browser is collecting the form fields' values.


Matthias

Andew Gee

unread,
Aug 22, 2009, 6:27:35 AM8/22/09
to Django users
I believe it wa the last of your suggestions, I was wrinting out the
fields of the main form individually and somehow they must of been
overwriting the inlineforms fields. I removed the loop that was doing
the main form and just used the form object and it worked. So i need
to go back and look over the fields to see why it was being
overwritten.

Thanks very much for your help.

Andrew

On Aug 22, 5:13 pm, Matthias Kestenholz
<matthias.kestenh...@gmail.com> wrote:

Andew Gee

unread,
Aug 22, 2009, 7:50:29 PM8/22/09
to Django users
Well...I don't know what happened but I have put my loop back in and
now it works. I cannot get the problem to reproduce. I must have had
something out of place but I don't know what, but it works now which
is what I need.

So again thanks for your help.

Andrew
Reply all
Reply to author
Forward
0 new messages