[1.11.x] Fixed #28058 -- Restored empty BoundFields evaluating to True.

Regression in b52c73008a

Backport of c09bf8d767 from master
This commit is contained in:
Tim Graham 2017-04-15 13:01:38 -04:00
parent 91bbe7b1c1
commit 844ae40a7c
3 changed files with 17 additions and 0 deletions

View File

@ -57,6 +57,13 @@ class BoundField(object):
for widget in self.field.widget.subwidgets(self.html_name, self.value(), attrs=attrs)
)
def __bool__(self):
# BoundField evaluates to True even if it doesn't have subwidgets.
return True
def __nonzero__(self): # Python 2 compatibility
return type(self).__bool__(self)
def __iter__(self):
return iter(self.subwidgets)

View File

@ -24,3 +24,6 @@ Bugfixes
* Fixed empty POST data table appearing instead of "No POST data" in HTML debug
page (:ticket:`28079`).
* Restored ``BoundField``\s without any ``choices`` evaluating to ``True``
(:ticket:`28058`).

View File

@ -745,6 +745,13 @@ Java</label></li>
[str(bf[1]), str(bf[2]), str(bf[3])],
)
def test_boundfield_bool(self):
"""BoundField without any choices (subwidgets) evaluates to True."""
class TestForm(Form):
name = ChoiceField(choices=[])
self.assertIs(bool(TestForm()['name']), True)
def test_forms_with_multiple_choice(self):
# MultipleChoiceField is a special case, as its data is required to be a list:
class SongForm(Form):