From 1213ef2b182261ee690eba484ac7a929ff4707c9 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 18 Jun 2016 10:33:37 -0400 Subject: [PATCH] [1.10.x] Refs #15667 -- Fixed crash when indexing RadioFieldRenderer with ModelChoiceIterator. Regression in 86573861a95e5a47dc7ff906443117d75b73dca1 --- django/forms/widgets.py | 2 +- tests/model_forms/tests.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index ab9794a1e78..604a6a401a0 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -693,7 +693,7 @@ class ChoiceFieldRenderer(object): self.choices = choices def __getitem__(self, idx): - choice = self.choices[idx] # Let the IndexError propagate + choice = list(self.choices)[idx] # Let the IndexError propagate return self.choice_input_class(self.name, self.value, self.attrs.copy(), choice, idx) def __str__(self): diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 37ae5089ae4..29472359c26 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -1573,6 +1573,13 @@ class ModelChoiceFieldTests(TestCase): with self.assertNumQueries(1): template.render(Context({'field': field})) + def test_modelchoicefield_index_renderer(self): + field = forms.ModelChoiceField(Category.objects.all(), widget=forms.RadioSelect) + self.assertEqual( + str(field.widget.get_renderer('foo', [])[0]), + '' + ) + class ModelMultipleChoiceFieldTests(TestCase): def setUp(self):