Fix ModelField max_length argument (#6773)

This commit is contained in:
Ryan P Kilby 2019-07-01 19:14:45 -07:00 committed by GitHub
parent a7778897ad
commit da06240257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -1888,9 +1888,9 @@ class ModelField(Field):
self.model_field = model_field self.model_field = model_field
# The `max_length` option is supported by Django's base `Field` class, # The `max_length` option is supported by Django's base `Field` class,
# so we'd better support it here. # so we'd better support it here.
max_length = kwargs.pop('max_length', None) self.max_length = kwargs.pop('max_length', None)
super().__init__(**kwargs) super().__init__(**kwargs)
if max_length is not None: if self.max_length is not None:
message = lazy_format(self.error_messages['max_length'], max_length=self.max_length) message = lazy_format(self.error_messages['max_length'], max_length=self.max_length)
self.validators.append( self.validators.append(
MaxLengthValidator(self.max_length, message=message)) MaxLengthValidator(self.max_length, message=message))

View File

@ -2204,8 +2204,8 @@ class TestBinaryJSONField(FieldValues):
field = serializers.JSONField(binary=True) field = serializers.JSONField(binary=True)
# Tests for FieldField. # Tests for FileField.
# --------------------- # --------------------
class MockRequest: class MockRequest:
def build_absolute_uri(self, value): def build_absolute_uri(self, value):
@ -2247,6 +2247,21 @@ class TestSerializerMethodField:
assert field.method_name == 'get_example_field' assert field.method_name == 'get_example_field'
# Tests for ModelField.
# ---------------------
class TestModelField:
def test_max_length_init(self):
field = serializers.ModelField(None)
assert len(field.validators) == 0
field = serializers.ModelField(None, max_length=10)
assert len(field.validators) == 1
# Tests for validation errors
# ---------------------------
class TestValidationErrorCode: class TestValidationErrorCode:
@pytest.mark.parametrize('use_list', (False, True)) @pytest.mark.parametrize('use_list', (False, True))
def test_validationerror_code_with_msg(self, use_list): def test_validationerror_code_with_msg(self, use_list):