diff --git a/django/forms/widgets.py b/django/forms/widgets.py index ab9794a1e7..604a6a401a 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 74ea1ad434..fce0656870 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -1593,6 +1593,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]), + '' + ) + def test_modelchoicefield_iterator(self): """ Iterator defaults to ModelChoiceIterator and can be overridden with