Fixed #15722: ensure formsets evaluate to True even if they have no forms. Thanks mlavin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16756 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2011-09-10 00:05:48 +00:00
parent 7bca049f1c
commit e061b036a5
2 changed files with 15 additions and 0 deletions

View File

@ -60,6 +60,10 @@ class BaseFormSet(StrAndUnicode):
def __len__(self): def __len__(self):
return len(self.forms) return len(self.forms)
def __nonzero__(self):
"""All formsets have a management form which is not included in the length"""
return True
def _management_form(self): def _management_form(self):
"""Returns the ManagementForm instance for this FormSet.""" """Returns the ManagementForm instance for this FormSet."""
if self.is_bound: if self.is_bound:

View File

@ -805,6 +805,17 @@ class FormsFormsetTestCase(TestCase):
self.assertEqual(str(reverse_formset[1]), str(forms[-2])) self.assertEqual(str(reverse_formset[1]), str(forms[-2]))
self.assertEqual(len(reverse_formset), len(forms)) self.assertEqual(len(reverse_formset), len(forms))
def test_formset_nonzero(self):
"""
Formsets with no forms should still evaluate as true.
Regression test for #15722
"""
ChoiceFormset = formset_factory(Choice, extra=0)
formset = ChoiceFormset()
self.assertEqual(len(formset.forms), 0)
self.assertTrue(formset)
data = { data = {
'choices-TOTAL_FORMS': '1', # the number of forms rendered 'choices-TOTAL_FORMS': '1', # the number of forms rendered
'choices-INITIAL_FORMS': '0', # the number of forms with initial data 'choices-INITIAL_FORMS': '0', # the number of forms with initial data