From 0ed2919814c80e31626dffdb6b80d0c20d43452f Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 3 Jan 2022 11:29:24 +0000 Subject: [PATCH] Fixed #33406 -- Avoided creation of MaxLengthValidator(None) when resolving Value.output_field for strings. This brings the behaviour in line with Field subclasses which append to the validators within __init__(), like BinaryField, and prevents the creation of a validator which incorrectly throws a TypeError, if it were used. --- django/db/models/fields/__init__.py | 3 ++- tests/expressions/tests.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index f30b523346..88c9ca9f28 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1010,7 +1010,8 @@ class CharField(Field): def __init__(self, *args, db_collation=None, **kwargs): super().__init__(*args, **kwargs) self.db_collation = db_collation - self.validators.append(validators.MaxLengthValidator(self.max_length)) + if self.max_length is not None: + self.validators.append(validators.MaxLengthValidator(self.max_length)) def check(self, **kwargs): databases = kwargs.get('databases') or [] diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index b622c3a9aa..9fd31d550c 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1859,6 +1859,7 @@ class ValueTests(TestCase): and this demonstrates that they don't throw an exception. """ value_types = [ + 'str', True, 42, 3.14,