Code

Ticket #14099: model-formset-delete.diff

File model-formset-delete.diff, 2.1 KB (added by kenth, 4 years ago)

patch for BaseModelFormSet to use BaseFormSet._should_delete_form

Line 
1diff --git a/django/forms/models.py b/django/forms/models.py
2index f2d514f..8f6b700 100644
3--- a/django/forms/models.py
4+++ b/django/forms/models.py
5@@ -16,7 +16,7 @@ from forms import BaseForm, get_declared_fields
6 from fields import Field, ChoiceField
7 from widgets import SelectMultiple, HiddenInput, MultipleHiddenInput
8 from widgets import media_property
9-from formsets import BaseFormSet, formset_factory, DELETION_FIELD_NAME
10+from formsets import BaseFormSet, formset_factory
11 
12 __all__ = (
13     'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model',
14@@ -609,13 +609,10 @@ class BaseModelFormSet(BaseFormSet):
15             pk_value = getattr(pk_value, 'pk', pk_value)
16 
17             obj = self._existing_object(pk_value)
18-            if self.can_delete:
19-                raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
20-                should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
21-                if should_delete:
22-                    self.deleted_objects.append(obj)
23-                    obj.delete()
24-                    continue
25+            if self.can_delete and self._should_delete_form (form):
26+                self.deleted_objects.append(obj)
27+                obj.delete()
28+                continue
29             if form.has_changed():
30                 self.changed_objects.append((obj, form.changed_data))
31                 saved_instances.append(self.save_existing(form, obj, commit=commit))
32@@ -630,11 +627,8 @@ class BaseModelFormSet(BaseFormSet):
33                 continue
34             # If someone has marked an add form for deletion, don't save the
35             # object.
36-            if self.can_delete:
37-                raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
38-                should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
39-                if should_delete:
40-                    continue
41+            if self.can_delete and self._should_delete_form (form):
42+                continue
43             self.new_objects.append(self.save_new(form, commit=commit))
44             if not commit:
45                 self.saved_forms.append(form)