Skip to content

Commit

Permalink
Fixed #24818 -- Prevented models.CharField from accepting a string as…
Browse files Browse the repository at this point in the history
… max_length
  • Loading branch information
alasdairnicol authored and timgraham committed May 19, 2015
1 parent ae1efb8 commit d091b75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,11 +1069,7 @@ def check(self, **kwargs):
return errors

def _check_max_length_attribute(self, **kwargs):
try:
max_length = int(self.max_length)
if max_length <= 0:
raise ValueError()
except TypeError:
if self.max_length is None:
return [
checks.Error(
"CharFields must define a 'max_length' attribute.",
Expand All @@ -1082,7 +1078,7 @@ def _check_max_length_attribute(self, **kwargs):
id='fields.E120',
)
]
except ValueError:
elif not isinstance(self.max_length, six.integer_types) or self.max_length <= 0:
return [
checks.Error(
"'max_length' must be a positive integer.",
Expand Down
16 changes: 16 additions & 0 deletions tests/invalid_models_tests/test_ordinary_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ class Model(models.Model):
]
self.assertEqual(errors, expected)

def test_str_max_length_value(self):
class Model(models.Model):
field = models.CharField(max_length='20')

field = Model._meta.get_field('field')
errors = field.check()
expected = [
Error(
"'max_length' must be a positive integer.",
hint=None,
obj=field,
id='fields.E121',
),
]
self.assertEqual(errors, expected)

def test_non_iterable_choices(self):
class Model(models.Model):
field = models.CharField(max_length=10, choices='bad')
Expand Down

0 comments on commit d091b75

Please sign in to comment.