Opened 11 years ago

Last modified 6 years ago

#12697 new New feature

Prevent deletion of some rows in a formset

Reported by: shadfc Owned by: nobody
Component: Forms Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Original post to dev mailing list:

I need to prevent deletion of some of the rows on an inline modelformset.

At first I thought I could just do some validation in clean() and
attach errors to the "DELETE" (really the field mapped to
forms.formsets.DELETION_FIELD_NAME), but
on the formset ignores errors on forms when can_delete is True.

A few possible solutions:

  1. Modify formset validation to ignore errors on the forms EXCEPT on the field referenced by forms.formsets.DELETION_FIELD_NAME ("DELETE" normally).
  1. Make the behavior of ignoring validation errors when can_delete is True explicit. Add a parameter such as "ignore_validation_on_delete" which defaults to True for backward compatibility. When False, formset validation fails on any errors.
  1. Allow can_delete to be specified per form in a formset, perhaps in a way similar to initial -- an iterable of boolean values.
  1. Add a pre-delete hook on model forms which can be used to circumvent deletion.

Change History (5)

comment:1 Changed 11 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:2 Changed 9 years ago by Matt McClanahan

Severity: Normal
Type: New feature

comment:3 Changed 8 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:4 Changed 8 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:5 Changed 6 years ago by Kaspars Sprogis

I found a very easy solution for this problem to avoid unwanted deletion of some inlines
You can just override delete_forms property method.

class MyInlineFormSet(BaseInlineFormSet):

    def deleted_forms(self):
        deleted_forms = super(MyInlineFormSet, self).deleted_forms

        for i, form in enumerate(deleted_forms):
            if some_criteria_to_prevent_deletion:

        return deleted_forms
Note: See TracTickets for help on using tickets.
Back to Top