From 9f2e8b5bb79722ccafa3c4d6816bc847be1f59f9 Mon Sep 17 00:00:00 2001 From: Carles Pina Estany Date: Tue, 25 Apr 2017 00:49:31 +0200 Subject: [PATCH] Fixed #28120 -- Checked that CharField.max_length is not a boolean. --- AUTHORS | 1 + django/db/models/fields/__init__.py | 3 ++- .../invalid_models_tests/test_ordinary_fields.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) 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')