[1.11.x] Fixed #27866 -- Made ChoiceWidget.format_value() return a list
Thanks Tim Graham for the review.
Backport of e487ffd3f0
from master.
This commit is contained in:
parent
ff0c6b83e5
commit
06d32f689e
|
@ -647,16 +647,10 @@ class ChoiceWidget(Widget):
|
||||||
return getter(name)
|
return getter(name)
|
||||||
|
|
||||||
def format_value(self, value):
|
def format_value(self, value):
|
||||||
"""Return selected values as a set."""
|
"""Return selected values as a list."""
|
||||||
if not isinstance(value, (tuple, list)):
|
if not isinstance(value, (tuple, list)):
|
||||||
value = [value]
|
value = [value]
|
||||||
values = set()
|
return [str(v) if v is not None else '' for v in value]
|
||||||
for v in value:
|
|
||||||
if v is None:
|
|
||||||
values.add('')
|
|
||||||
else:
|
|
||||||
values.add(force_text(v))
|
|
||||||
return values
|
|
||||||
|
|
||||||
|
|
||||||
class Select(ChoiceWidget):
|
class Select(ChoiceWidget):
|
||||||
|
|
|
@ -7,6 +7,12 @@ class SelectMultipleTest(WidgetTest):
|
||||||
widget = SelectMultiple
|
widget = SelectMultiple
|
||||||
numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra'))
|
numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra'))
|
||||||
|
|
||||||
|
def test_format_value(self):
|
||||||
|
widget = self.widget(choices=self.numeric_choices)
|
||||||
|
self.assertEqual(widget.format_value(None), [''])
|
||||||
|
self.assertEqual(widget.format_value(''), [''])
|
||||||
|
self.assertEqual(widget.format_value([3, 0, 1]), ['3', '0', '1'])
|
||||||
|
|
||||||
def test_render_selected(self):
|
def test_render_selected(self):
|
||||||
self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=(
|
self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=(
|
||||||
"""<select multiple="multiple" name="beatles">
|
"""<select multiple="multiple" name="beatles">
|
||||||
|
|
Loading…
Reference in New Issue