diff --git a/django/utils/choices.py b/django/utils/choices.py index 7f40bce5104d..6b355d2324f5 100644 --- a/django/utils/choices.py +++ b/django/utils/choices.py @@ -21,8 +21,9 @@ def __eq__(self, other): return super().__eq__(other) def __getitem__(self, index): - if index < 0: - # Suboptimally consume whole iterator to handle negative index. + if isinstance(index, slice) or index < 0: + # Suboptimally consume whole iterator to handle slices and negative + # indexes. return list(self)[index] try: return next(islice(self, index, index + 1)) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 36e54d4b8b1a..3d856d36c56d 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -183,6 +183,33 @@ def test_choices(self): self.choices_from_callable.choices.func(), [(0, "0"), (1, "1"), (2, "2")] ) + def test_choices_slice(self): + for choices, expected_slice in [ + (self.empty_choices.choices, []), + (self.empty_choices_bool.choices, []), + (self.empty_choices_text.choices, []), + (self.with_choices.choices, [(1, "A")]), + (self.with_choices_dict.choices, [(1, "A")]), + (self.with_choices_nested_dict.choices, [("Thing", [(1, "A")])]), + (self.choices_from_iterator.choices, [(0, "0"), (1, "1")]), + (self.choices_from_callable.choices.func(), [(0, "0"), (1, "1")]), + (self.choices_from_callable.choices, [(0, "0"), (1, "1")]), + ]: + with self.subTest(choices=choices): + self.assertEqual(choices[:2], expected_slice) + + def test_choices_negative_index(self): + for choices, expected_choice in [ + (self.with_choices.choices, (1, "A")), + (self.with_choices_dict.choices, (1, "A")), + (self.with_choices_nested_dict.choices, ("Thing", [(1, "A")])), + (self.choices_from_iterator.choices, (2, "2")), + (self.choices_from_callable.choices.func(), (2, "2")), + (self.choices_from_callable.choices, (2, "2")), + ]: + with self.subTest(choices=choices): + self.assertEqual(choices[-1], expected_choice) + def test_flatchoices(self): self.assertEqual(self.no_choices.flatchoices, []) self.assertEqual(self.empty_choices.flatchoices, [])