Opened 12 years ago
Closed 12 years ago
#19145 closed Bug (duplicate)
valid formset with invalid deleted form raises AttributeError when trying to access cleaned_data
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | 1.4 |
Severity: | Normal | Keywords: | formset cleaned_data |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Example (run in manage.py shell
):
from django.forms import Form, BooleanField from django.forms.formsets import formset_factory class F(Form): a = BooleanField() FS = formset_factory(form = F, can_delete = True) fs = FS(data = { 'form-MAX_NUM_FORMS': '', 'form-INITIAL_FORMS': '1', 'form-TOTAL_FORMS': '1', 'form-0-a': '', 'form-0-DELETE': 'on', }) assert fs.is_valid() # fs.forms[0].cleaned_data raises AttributeError print fs.cleaned_data
This can be fixed by changing BaseFormSet._get_cleaned_data
to only return the cleaned_data
of non-deleted forms:
def _get_cleaned_data(self): """ Returns a list of form.cleaned_data dicts for every form in self.forms. """ if not self.is_valid(): raise AttributeError("'%s' object has no attribute 'cleaned_data'" % self.__class__.__name__) return [form.cleaned_data for form in self.forms if not self._should_delete_form(form)]
Note:
See TracTickets
for help on using tickets.
This should have been fixed recently (see #5524, https://github.com/django/django/commit/121fd109de09ece4e263e508f9034df9b583da46). cleaned_data is now always available once a form has been validated. Reopen if I missed the point.