DEV Community

Discussion on: Django inline formsets with Class-based views and crispy forms

zxenia profile image
Xenia Author • Edited on

Hi Gonçalo,

Thanks for the question!
You are right, you will need to use nested form inside each form in formset.
For this you need to create CollectionTitleChildFormSet and pass it in add_fields() method when overriding BaseInlineFormSet. Therefore the CollectionTitleFormSet will look different because now we need to set formset explicitly to BaseTitleChildFormset (check out my commit and this blog post).
The backend logic works fine for me but I didn't figure out how to implement dynamic 'add child': it should work like this - everytime I add a new Title the row with an empty Title and a row with at least One Child followed by button 'add child' is added.
If you figure it out I will be interested to know how ;)

gbritoo27 profile image
Gonçalo Brito

Thanks for the quick reply!

I have my backend working as well. I believe that the jQuery library (django-dynamic-formset) is not prepared for this.
What you're doing with this library on the nested formsets is creating 1 formset with all the childs, and I believe this should create 1 formset with the childs on each title.
I also think your childs' prefix is wrong, let me know what prefix appears on the class when you use "formset_child-(( formset.prefix ))". I'm personally using formset_child-(( nested_form.prefix )) and it joins the formset (title) prefix with the childs' prefix, something like title-0-child-0, title-0-child-1.

I will spend the rest of the day trying to fix this, I'll let you know how it goes.