diff options
| author | Claude Paroz <claude@2xlibre.net> | 2018-03-30 11:55:33 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-04-02 09:17:38 -0400 |
| commit | 160829d35b54a97977bf8ce5588aa19b1cfc2b43 (patch) | |
| tree | 2147059cbfb715cf3c3a038081d4f2134380b454 | |
| parent | 327882a2456e84ed10668441b5f64f157f192093 (diff) | |
[2.0.x] Fixed #29273 -- Prevented initial selection of empty choice in multiple choice widgets.
Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.
Backport of f3b69f9757ec03057441ebbd52b7cdbfed31fb32 from master.
| -rw-r--r-- | django/forms/widgets.py | 2 | ||||
| -rw-r--r-- | docs/releases/1.11.12.txt | 4 | ||||
| -rw-r--r-- | docs/releases/2.0.4.txt | 4 | ||||
| -rw-r--r-- | tests/forms_tests/widget_tests/test_checkboxselectmultiple.py | 6 | ||||
| -rw-r--r-- | tests/forms_tests/widget_tests/test_selectmultiple.py | 10 |
5 files changed, 20 insertions, 6 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 47396213d9..976487cb23 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -658,6 +658,8 @@ class ChoiceWidget(Widget): def format_value(self, value): """Return selected values as a list.""" + if value is None and self.allow_multiple_selected: + return [] if not isinstance(value, (tuple, list)): value = [value] return [str(v) if v is not None else '' for v in value] diff --git a/docs/releases/1.11.12.txt b/docs/releases/1.11.12.txt index 64130c065e..186c18be58 100644 --- a/docs/releases/1.11.12.txt +++ b/docs/releases/1.11.12.txt @@ -12,3 +12,7 @@ Bugfixes * Fixed a regression in Django 1.11.8 where combining two annotated ``values_list()`` querysets with ``union()``, ``difference()``, or ``intersection()`` crashed due to mismatching columns (:ticket:`29229`). + +* Fixed a regression in Django 1.11 where an empty choice could be initially + selected for the ``SelectMultiple`` and ``CheckboxSelectMultiple`` widgets + (:ticket:`29273`) diff --git a/docs/releases/2.0.4.txt b/docs/releases/2.0.4.txt index d27ccf98bc..c0b51d257f 100644 --- a/docs/releases/2.0.4.txt +++ b/docs/releases/2.0.4.txt @@ -25,3 +25,7 @@ Bugfixes * Fixed a regression in Django 1.11.8 where combining two annotated ``values_list()`` querysets with ``union()``, ``difference()``, or ``intersection()`` crashed due to mismatching columns (:ticket:`29229`). + +* Fixed a regression in Django 1.11 where an empty choice could be initially + selected for the ``SelectMultiple`` and ``CheckboxSelectMultiple`` widgets + (:ticket:`29273`) diff --git a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py index 239f80da47..1942d32970 100644 --- a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py +++ b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py @@ -32,10 +32,12 @@ class CheckboxSelectMultipleTest(WidgetTest): def test_render_none(self): """ - If the value is None, none of the options are selected. + If the value is None, none of the options are selected, even if the + choices have an empty option. """ - self.check_html(self.widget(choices=self.beatles), 'beatles', None, html=( + self.check_html(self.widget(choices=(('', 'Unknown'),) + self.beatles), 'beatles', None, html=( """<ul> + <li><label><input type="checkbox" name="beatles" value="" /> Unknown</label></li> <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li> diff --git a/tests/forms_tests/widget_tests/test_selectmultiple.py b/tests/forms_tests/widget_tests/test_selectmultiple.py index 149fae6d4b..3897e36888 100644 --- a/tests/forms_tests/widget_tests/test_selectmultiple.py +++ b/tests/forms_tests/widget_tests/test_selectmultiple.py @@ -9,7 +9,7 @@ class SelectMultipleTest(WidgetTest): def test_format_value(self): widget = self.widget(choices=self.numeric_choices) - self.assertEqual(widget.format_value(None), ['']) + self.assertEqual(widget.format_value(None), []) self.assertEqual(widget.format_value(''), ['']) self.assertEqual(widget.format_value([3, 0, 1]), ['3', '0', '1']) @@ -35,10 +35,12 @@ class SelectMultipleTest(WidgetTest): def test_render_none(self): """ - If the value is None, none of the options are selected. + If the value is None, none of the options are selected, even if the + choices have an empty option. """ - self.check_html(self.widget(choices=self.beatles), 'beatles', None, html=( - """<select multiple="multiple" name="beatles"> + self.check_html(self.widget(choices=(('', 'Unknown'),) + self.beatles), 'beatles', None, html=( + """<select multiple name="beatles"> + <option value="">Unknown</option> <option value="J">John</option> <option value="P">Paul</option> <option value="G">George</option> |
