mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-14 13:46:59 +03:00
Fix ModelField max_length argument (#6773)
This commit is contained in:
parent
a7778897ad
commit
da06240257
|
@ -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))
|
||||||
|
|
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user