Code

Ticket #18751: 10711-2.diff

File 10711-2.diff, 1.6 KB (added by charettes, 20 months ago)

claudep's patch for #10711 which happens to fix this issue

Line 
1diff --git a/django/forms/formsets.py b/django/forms/formsets.py
2index 1ec8340..d652130 100644
3--- a/django/forms/formsets.py
4+++ b/django/forms/formsets.py
5@@ -251,13 +251,7 @@ class BaseFormSet(StrAndUnicode):
6     errors = property(_get_errors)
7 
8     def _should_delete_form(self, form):
9-        # The way we lookup the value of the deletion field here takes
10-        # more code than we'd like, but the form's cleaned_data will
11-        # not exist if the form is invalid.
12-        field = form.fields[DELETION_FIELD_NAME]
13-        raw_value = form._raw_value(DELETION_FIELD_NAME)
14-        should_delete = field.clean(raw_value)
15-        return should_delete
16+        return form.cleaned_data.get(DELETION_FIELD_NAME, False)
17 
18     def is_valid(self):
19         """
20diff --git a/django/forms/models.py b/django/forms/models.py
21index e6ae357..553f174 100644
22--- a/django/forms/models.py
23+++ b/django/forms/models.py
24@@ -596,6 +596,10 @@ class BaseModelFormSet(BaseFormSet):
25             return []
26 
27         saved_instances = []
28+        try:
29+            forms_to_delete = self.deleted_forms
30+        except AttributeError:
31+            forms_to_delete = []
32         for form in self.initial_forms:
33             pk_name = self._pk_field.name
34             raw_pk_value = form._raw_value(pk_name)
35@@ -606,7 +610,7 @@ class BaseModelFormSet(BaseFormSet):
36             pk_value = getattr(pk_value, 'pk', pk_value)
37 
38             obj = self._existing_object(pk_value)
39-            if self.can_delete and self._should_delete_form(form):
40+            if form in forms_to_delete:
41                 self.deleted_objects.append(obj)
42                 obj.delete()
43                 continue