Formset with _some_ deletable rows

23 views
Skip to first unread message

shadfc

unread,
Jan 25, 2010, 4:22:36 PM1/25/10
to Django developers
So the short of it is that 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 field mapped to
forms.formsets.DELETION_FIELD_NAME, but is_valid() [http://
code.djangoproject.com/browser/django/trunk/django/forms/formsets.py?
rev=#L216] on the formset ignores errors on forms when can_delete is
True.

I thought I'd bring it up here for discussion on the proper way to
handle this. A few ideas come to my mind, though I won't say they are
good ones:

1) Modify formset validation to ignore errors on the forms EXCEPT
on the field referenced by DELETION_FIELD_NAME when can_delete=True.
Right now, you cannot do any validation on the delete field when its
value is True (well, you can but it gets ignored and the form gets
deleted).

2) 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.

3) Allow can_delete to be specified per form in a formset, perhaps
in a way similar to initial -- an iterable of boolean values.

4) Add a pre-delete hook on model forms which can circumvent
deletion (return False?).

As it stands, the only options I see right now are custom form
renderings (not safe from malicious users), and parsing/modifying the
POST data before it his the formset (ugly), or use a custom
BaseFormSet and override is_valid() with method #1 from above.

Jay

Reply all
Reply to author
Forward
0 new messages