diff --git a/AUTHORS b/AUTHORS index 7c211983d6..8f4732d986 100644 --- a/AUTHORS +++ b/AUTHORS @@ -140,6 +140,7 @@ answer newbie questions, and generally made Django that much better: Cameron Knight (ckknight) Can Burak Çilingir Carl Meyer + Carles Pina i Estany Carlos Eduardo de Paula Carlos Matías de la Torre Carlton Gibson diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 77117009ab..95630ebb4e 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1040,7 +1040,8 @@ class CharField(Field): id='fields.E120', ) ] - elif not isinstance(self.max_length, int) or self.max_length <= 0: + elif (not isinstance(self.max_length, int) or isinstance(self.max_length, bool) or + self.max_length <= 0): return [ checks.Error( "'max_length' must be a positive integer.", diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 9b5b57834d..90f4b31902 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -139,6 +139,21 @@ class CharFieldTests(TestCase): ] self.assertEqual(errors, expected) + def test_str_max_length_type(self): + class Model(models.Model): + field = models.CharField(max_length=True) + + field = Model._meta.get_field('field') + errors = field.check() + expected = [ + Error( + "'max_length' must be a positive integer.", + 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')