1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #29273 -- Prevented initial selection of empty choice in multiple choice widgets.

Regression in b52c73008a.
This commit is contained in:
Claude Paroz 2018-03-30 11:55:33 +02:00 committed by Tim Graham
parent d31a248c64
commit f3b69f9757
5 changed files with 19 additions and 5 deletions

View File

@ -649,6 +649,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]

View File

@ -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`)

View File

@ -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`)

View File

@ -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>

View File

@ -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=(
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>