Opened 6 years ago

Closed 6 years ago

#19145 closed Bug (duplicate)

valid formset with invalid deleted form raises AttributeError when trying to access cleaned_data

Reported by: valtron2000@… 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


Example (run in 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)]

Change History (1)

comment:1 Changed 6 years ago by Claude Paroz

Resolution: duplicate
Status: newclosed

This should have been fixed recently (see #5524, cleaned_data is now always available once a form has been validated. Reopen if I missed the point.

Note: See TracTickets for help on using tickets.
Back to Top