Making a Form with Many-To-Many Relationships and Multiple Forms Inside

Adding a new instance to the already-displayed page requires a little bit of JavaScript. There are a couple different ways of handling this.

What we do when the server is preparing the page for display, we use the formset.empty_form property to get an empty copy of the form to be stored in a hidden div. The Add Item button makes a copy of that empty form, replaces the __prefix__ with the value of forms-TOTAL_FORMS, increments forms-TOTAL_FORMS, and appends the copied form into the div where the forms are displayed.

I know there are a number of blog posts available if you search for “django add form to formset” - I don’t have any good references handy.

Ken

1 Like