mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #24818 -- Prevented models.CharField from accepting a string as max_length
This commit is contained in:
parent
ae1efb853c
commit
d091b75eef
@ -1069,11 +1069,7 @@ class CharField(Field):
|
|||||||
return errors
|
return errors
|
||||||
|
|
||||||
def _check_max_length_attribute(self, **kwargs):
|
def _check_max_length_attribute(self, **kwargs):
|
||||||
try:
|
if self.max_length is None:
|
||||||
max_length = int(self.max_length)
|
|
||||||
if max_length <= 0:
|
|
||||||
raise ValueError()
|
|
||||||
except TypeError:
|
|
||||||
return [
|
return [
|
||||||
checks.Error(
|
checks.Error(
|
||||||
"CharFields must define a 'max_length' attribute.",
|
"CharFields must define a 'max_length' attribute.",
|
||||||
@ -1082,7 +1078,7 @@ class CharField(Field):
|
|||||||
id='fields.E120',
|
id='fields.E120',
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
except ValueError:
|
elif not isinstance(self.max_length, six.integer_types) or self.max_length <= 0:
|
||||||
return [
|
return [
|
||||||
checks.Error(
|
checks.Error(
|
||||||
"'max_length' must be a positive integer.",
|
"'max_length' must be a positive integer.",
|
||||||
|
@ -129,6 +129,22 @@ class CharFieldTests(IsolatedModelsTestCase):
|
|||||||
]
|
]
|
||||||
self.assertEqual(errors, expected)
|
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):
|
def test_non_iterable_choices(self):
|
||||||
class Model(models.Model):
|
class Model(models.Model):
|
||||||
field = models.CharField(max_length=10, choices='bad')
|
field = models.CharField(max_length=10, choices='bad')
|
||||||
|
Loading…
Reference in New Issue
Block a user