From e487ffd3f0f9931e7c18bfbd47cf1e6b059ddc41 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 21 Feb 2017 13:27:29 +0100 Subject: [PATCH] Fixed #27866 -- Made ChoiceWidget.format_value() return a list Thanks Tim Graham for the review. --- django/forms/widgets.py | 10 ++-------- tests/forms_tests/widget_tests/test_selectmultiple.py | 6 ++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 2e58d449c4..bb200dcc87 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -628,16 +628,10 @@ class ChoiceWidget(Widget): return getter(name) def format_value(self, value): - """Return selected values as a set.""" + """Return selected values as a list.""" if not isinstance(value, (tuple, list)): value = [value] - values = set() - for v in value: - if v is None: - values.add('') - else: - values.add(force_text(v)) - return values + return [str(v) if v is not None else '' for v in value] class Select(ChoiceWidget): diff --git a/tests/forms_tests/widget_tests/test_selectmultiple.py b/tests/forms_tests/widget_tests/test_selectmultiple.py index 71259b9847..149fae6d4b 100644 --- a/tests/forms_tests/widget_tests/test_selectmultiple.py +++ b/tests/forms_tests/widget_tests/test_selectmultiple.py @@ -7,6 +7,12 @@ class SelectMultipleTest(WidgetTest): widget = SelectMultiple 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): self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=( """