Opened 14 years ago

Last modified 10 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

Description

Original post to dev mailing list: http://groups.google.com/group/django-developers/browse_frm/thread/5008b34e29c80417

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
is_valid()
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 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Matt McClanahan, 13 years ago

Severity: Normal
Type: New feature

comment:3 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:4 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:5 by Kaspars Sprogis, 10 years ago

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):

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

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

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