Fixed #18751 -- Cleaned up BaseFormSet._should_delete_form

We can do that now that cleaned_data is guaranteed to be
present. Related to [121fd109].
Thanks Simon Charette for his work on the ticket.
This commit is contained in:
Claude Paroz 2012-08-30 15:51:13 +02:00
parent ae88e73fa6
commit fb3d916c20
2 changed files with 9 additions and 8 deletions

View File

@ -254,13 +254,10 @@ class BaseFormSet(object):
errors = property(_get_errors) errors = property(_get_errors)
def _should_delete_form(self, form): def _should_delete_form(self, form):
# The way we lookup the value of the deletion field here takes """
# more code than we'd like, but the form's cleaned_data will Returns whether or not the form was marked for deletion.
# not exist if the form is invalid. """
field = form.fields[DELETION_FIELD_NAME] return form.cleaned_data.get(DELETION_FIELD_NAME, False)
raw_value = form._raw_value(DELETION_FIELD_NAME)
should_delete = field.clean(raw_value)
return should_delete
def is_valid(self): def is_valid(self):
""" """

View File

@ -591,6 +591,10 @@ class BaseModelFormSet(BaseFormSet):
return [] return []
saved_instances = [] saved_instances = []
try:
forms_to_delete = self.deleted_forms
except AttributeError:
forms_to_delete = []
for form in self.initial_forms: for form in self.initial_forms:
pk_name = self._pk_field.name pk_name = self._pk_field.name
raw_pk_value = form._raw_value(pk_name) raw_pk_value = form._raw_value(pk_name)
@ -601,7 +605,7 @@ class BaseModelFormSet(BaseFormSet):
pk_value = getattr(pk_value, 'pk', pk_value) pk_value = getattr(pk_value, 'pk', pk_value)
obj = self._existing_object(pk_value) obj = self._existing_object(pk_value)
if self.can_delete and self._should_delete_form(form): if form in forms_to_delete:
self.deleted_objects.append(obj) self.deleted_objects.append(obj)
obj.delete() obj.delete()
continue continue