Skip to content

Commit

Permalink
Fixed #29698 -- Fixed Field._check_choices() crash on invalid choices.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmichea authored and timgraham committed Aug 22, 2018
1 parent 50b8493 commit 7def8be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def is_value(value, accept_promise=True):
for choices_group in self.choices:
try:
group_name, group_choices = choices_group
except ValueError:
except (TypeError, ValueError):
# Containing non-pairs
break
try:
Expand Down
22 changes: 14 additions & 8 deletions tests/invalid_models_tests/test_ordinary_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,20 @@ def test_choices_containing_non_pairs(self):
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):
Expand Down

0 comments on commit 7def8be

Please sign in to comment.