diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 68abd3b7dcc..ab9794a1e78 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -753,20 +753,19 @@ class RendererMixin(object): self.renderer = renderer super(RendererMixin, self).__init__(*args, **kwargs) - def subwidgets(self, name, value, attrs=None, choices=()): - for widget in self.get_renderer(name, value, attrs, choices): + def subwidgets(self, name, value, attrs=None): + for widget in self.get_renderer(name, value, attrs): yield widget - def get_renderer(self, name, value, attrs=None, choices=()): + def get_renderer(self, name, value, attrs=None): """Returns an instance of the renderer.""" if value is None: value = self._empty_value final_attrs = self.build_attrs(attrs) - choices = list(chain(self.choices, choices)) - return self.renderer(name, value, final_attrs, choices) + return self.renderer(name, value, final_attrs, self.choices) - def render(self, name, value, attrs=None, choices=()): - return self.get_renderer(name, value, attrs, choices).render() + def render(self, name, value, attrs=None): + return self.get_renderer(name, value, attrs).render() def id_for_label(self, id_): # Widgets using this RendererMixin are made of a collection of diff --git a/tests/forms_tests/tests/test_widgets.py b/tests/forms_tests/tests/test_widgets.py index 8f84ddcb9bf..b7b228d30aa 100644 --- a/tests/forms_tests/tests/test_widgets.py +++ b/tests/forms_tests/tests/test_widgets.py @@ -23,8 +23,8 @@ class FormsWidgetTests(SimpleTestCase): # RadioSelect uses a RadioFieldRenderer to render the individual radio inputs. # You can manipulate that object directly to customize the way the RadioSelect # is rendered. - w = RadioSelect() - r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + r = w.get_renderer('beatle', 'J') inp_set1 = [] inp_set2 = [] inp_set3 = [] @@ -62,8 +62,8 @@ beatle J G George False beatle J R Ringo False""") # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput - w = RadioSelect() - r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + r = w.get_renderer('beatle', 'J') self.assertHTMLEqual(str(r[1]), '') self.assertHTMLEqual( str(r[0]), @@ -86,9 +86,9 @@ beatle J R Ringo False""") class MyRenderer(RadioFieldRenderer): def render(self): return '
\n'.join(six.text_type(choice) for choice in self) - w = RadioSelect(renderer=MyRenderer) + w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), renderer=MyRenderer) self.assertHTMLEqual( - w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), + w.render('beatle', 'G'), """


@@ -98,9 +98,9 @@ beatle J R Ringo False""") # Or you can use custom RadioSelect fields that use your custom renderer. class CustomRadioSelect(RadioSelect): renderer = MyRenderer - w = CustomRadioSelect() + w = CustomRadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) self.assertHTMLEqual( - w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), + w.render('beatle', 'G'), """


@@ -112,10 +112,8 @@ beatle J R Ringo False""") # str is just to test some Python 2 issue with bytestrings outer_html = str('{content}') inner_html = '

{choice_value}{sub_widgets}

' - w = RadioSelect(renderer=MyRenderer) - output = w.render('beatle', 'J', - choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), - attrs={'id': 'bar'}) + w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), renderer=MyRenderer) + output = w.render('beatle', 'J', attrs={'id': 'bar'}) self.assertIsInstance(output, SafeData) self.assertHTMLEqual( output, @@ -128,17 +126,36 @@ beatle J R Ringo False""") def test_subwidget(self): # Each subwidget tag gets a separate ID when the widget has an ID specified - self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """ + self.assertHTMLEqual( + "\n".join( + c.tag() for c in CheckboxSelectMultiple( + attrs={'id': 'abc'}, + choices=zip(list('abc'), list('ABC')) + ).subwidgets('letters', list('ac')) + ), + """ """) # Each subwidget tag does not get an ID if the widget does not have an ID specified - self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple().subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """ + self.assertHTMLEqual( + "\n".join(c.tag() for c in CheckboxSelectMultiple( + choices=zip(list('abc'), list('ABC')), + ).subwidgets('letters', list('ac'))), + """ """) # The id_for_label property of the subwidget should return the ID that is used on the subwidget's tag - self.assertHTMLEqual("\n".join('' % (c.choice_value, c.id_for_label) for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', [], choices=zip(list('abc'), list('ABC')))), """ + self.assertHTMLEqual( + "\n".join( + '' + % (c.choice_value, c.id_for_label) for c in CheckboxSelectMultiple( + attrs={'id': 'abc'}, + choices=zip(list('abc'), list('ABC')), + ).subwidgets('letters', []) + ), + """ """) diff --git a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py index 9e594fba37c..1e9d31e6a7a 100644 --- a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py +++ b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py @@ -4,10 +4,10 @@ from .base import WidgetTest class CheckboxSelectMultipleTest(WidgetTest): - widget = CheckboxSelectMultiple() + widget = CheckboxSelectMultiple def test_render_value(self): - self.check_html(self.widget, 'beatles', ['J'], choices=self.beatles, html=( + self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=( """ """ - self.check_html(self.widget, 'beatle', 'J', choices=self.beatles, attrs={'id': 'bar'}, html=html) + self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', attrs={'id': 'bar'}, html=html)