I'm using inline formsets in Django, and for each item showing one "extra" form, for adding another object.
The forms for existing objects have "Delete" checkboxes, for removing that object, which makes sense.
But also the "extra" forms have these "Delete" checkboxes... which makes no sense because there's nothing there to delete. Inline forms in the Django admin don't show these "Delete" checkboxes for "extra" forms.
How can I remove these checkboxes on the "extra" inline forms?
The inline formsets part of my template is something like this (simplified, full version on GitHub):
{% for bookimage_form in form.forms %}
{% for hidden_field in bookimage_form.hidden_fields %}
{{ hidden_field.errors }}
{% endfor %}
{{ bookimage_form.as_table }}
{% endfor %}
And here's the "Delete" checkbox that seems superfluous:
You can use the can_delete setting of the InlineModelAdmin
class (TabularInline
inherits from InlineModelAdmin
):
class BookImageInline(admin.TabularInline):
model = BookImage
extra = 1
can_delete = False
Update for Django 3.2+ (link), you can now pass in can_delete_extra
as False to formset_factory or it extended classes to remove checkbox from extra forms
can_delete_extra New in Django 3.2.
BaseFormSet.can_delete_extra
Default: True
While setting can_delete=True, specifying can_delete_extra=False will remove the option to delete extra forms.
For anyone having Django version under 3.2 and do not wish to upgrade, please use the following method of overriding the BaseFormSet:
class CustomFormSetBase(BaseModelFormSet):
def add_fields(self, form, index):
super().add_fields(form, index)
if 'DELETE' in form.fields and form.instance.pk: # check if have instance
form.fields['DELETE'] = forms.BooleanField(
label=_('Delete'),
widget=forms.CheckboxInput(
attrs={
'class': 'form-check-input'
}
),
required=False
)
else:
form.fields.pop('DELETE', None)
YourFormSet = modelformset_factory(
formset=CustomFormSetBase,
can_delete=True,
extra=2
)
only took them 13 years to add this >.> https://code.djangoproject.com/ticket/9061
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With