Commit 1213ef2b authored by Tim Graham's avatar Tim Graham
Browse files

[1.10.x] Refs #15667 -- Fixed crash when indexing RadioFieldRenderer with ModelChoiceIterator.

Regression in 86573861
parent bdc29b71
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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):
+7 −0
Original line number Diff line number Diff line
@@ -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]),
            '<label><input name="foo" type="radio" value="" /> ---------</label>'
        )


class ModelMultipleChoiceFieldTests(TestCase):
    def setUp(self):