nspo
nspo

Reputation: 1518

Django crispy form with formset does not show delete checkbox

I have some code structure very similar to this example: https://gist.github.com/ibarovic/3092910

Everything is working pretty well, except that the delete checkbox after each Book element is not shown at all. If I change

{% crispy formset formset.form.helper %}

to

{% crispy formset %}

the delete checkboxes are shown, but the form does not work anymore (b/c the HTML form tags are used not only once).

I suspect that those checkboxes are not known to the BookForm (b/c they somehow get added later by the inlineformset_factory), so they do not get added to the layout and are ignored. I have no idea how to circumvent that though.

Upvotes: 1

Views: 2236

Answers (1)

nspo
nspo

Reputation: 1518

I found a solution thanks to this: Django crispy-forms, BaseGenericInlineFormSet & allow_delete

In the BookForm (which gets repeated many times) init, you can add something like:

self.helper = FormHelper(form=self) # or manual layout
self.helper.form_tag = False

i = self.helper.layout.fields.index('title')
self.helper.layout.insert(i+1, layout.Field("DELETE"))

The capital letters "DELETE" field then gets added to each single BookForm...

Upvotes: 7

Related Questions