Fixed #29698 -- Fixed Field._check_choices() crash on invalid choices.

This commit is contained in:
Franck Michea 2018-08-21 22:35:25 +02:00 committed by Tim Graham
parent 50b8493581
commit 7def8bed58
2 changed files with 15 additions and 9 deletions

View File

@ -260,7 +260,7 @@ class Field(RegisterLookupMixin):
for choices_group in self.choices:
try:
group_name, group_choices = choices_group
except ValueError:
except (TypeError, ValueError):
# Containing non-pairs
break
try:

View File

@ -174,14 +174,20 @@ class CharFieldTests(TestCase):
class Model(models.Model):
field = models.CharField(max_length=10, choices=[(1, 2, 3), (1, 2, 3)])
field = Model._meta.get_field('field')
self.assertEqual(field.check(), [
Error(
"'choices' must be an iterable containing (actual value, human readable name) tuples.",
obj=field,
id='fields.E005',
),
])
class Model2(models.Model):
field = models.IntegerField(choices=[0])
for model in (Model, Model2):
with self.subTest(model.__name__):
field = model._meta.get_field('field')
self.assertEqual(field.check(), [
Error(
"'choices' must be an iterable containing (actual "
"value, human readable name) tuples.",
obj=field,
id='fields.E005',
),
])
def test_choices_containing_lazy(self):
class Model(models.Model):