Django Formset without instance
Asked Answered
K

2

10

In this Django Doc explain how to create a formset that allows you to edit books belonging to a particular author.

What I want to do is: Create a formset that allows you to ADD new book belonging to a NEW author... Add the Book and their Authors in the same formset.

Can you gime a light? thanks.

Kennie answered 12/8, 2009 at 18:14 Comment(0)
S
13

When you're instantiating the form and formset for the initial display, you don't need to provide an instance - so you will just get blank forms.

When you pass in the data on POST, you can do the form first, save it, and get an instance. Then you can pass that instance into the formset, so that it correctly saves the related objects.

Sunstone answered 12/8, 2009 at 22:47 Comment(1)
Thanks, lights: if request.method == "POST": bookform = BookForm(request.POST) b = bookform .save(commit=False) b.save() formset = PadreFormSet(request.POST, request.FILES, instance=b) if formset.is_valid(): formset.save() It worked for me, Thanks!Kennie
C
0

This depends on whether you're doing it yourself, or using the built-in admin.

If you're using the admin, you can use inlines.

If you're doing this in your own application, then it's up to you. Create a single form which has fields for a new author and book. When the user submits the form, it's your job to create the new records.

Controvert answered 12/8, 2009 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.