mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 11:00:13 +03:00
add feature:
extra_validators in serializer mete class
This commit is contained in:
parent
8f9b875456
commit
023405b6a2
|
@ -1026,6 +1026,8 @@ class ModelSerializer(Serializer):
|
|||
field_names, declared_fields, extra_kwargs
|
||||
)
|
||||
|
||||
extra_validators = self.get_extra_validators()
|
||||
|
||||
# Determine the fields that should be included on the serializer.
|
||||
fields = OrderedDict()
|
||||
|
||||
|
@ -1050,6 +1052,13 @@ class ModelSerializer(Serializer):
|
|||
field_kwargs, extra_field_kwargs
|
||||
)
|
||||
|
||||
extra_field_validators = extra_validators.get(field_name, [])
|
||||
|
||||
# Include extra validators defined in `Meta.validators`
|
||||
field_kwargs = self.include_extra_validators(
|
||||
field_kwargs, extra_field_validators
|
||||
)
|
||||
|
||||
# Create the serializer field.
|
||||
fields[field_name] = field_class(**field_kwargs)
|
||||
|
||||
|
@ -1431,6 +1440,25 @@ class ModelSerializer(Serializer):
|
|||
|
||||
return extra_kwargs, hidden_fields
|
||||
|
||||
def include_extra_validators(self, kwargs, extra_validators):
|
||||
"""
|
||||
Include any 'extra_validators' that have been included for this field,
|
||||
"""
|
||||
print(kwargs.get('validators', []))
|
||||
print(extra_validators)
|
||||
validators = kwargs.get('validators', []) + extra_validators
|
||||
if validators:
|
||||
kwargs['validators'] = validators
|
||||
return kwargs
|
||||
|
||||
def get_extra_validators(self):
|
||||
"""
|
||||
Return a dictionary mapping field names to a list of
|
||||
additional validators.
|
||||
"""
|
||||
extra_validators = copy.deepcopy(getattr(self.Meta, 'extra_validators', {}))
|
||||
return extra_validators
|
||||
|
||||
def _get_model_fields(self, field_names, declared_fields, extra_kwargs):
|
||||
"""
|
||||
Returns all the model fields that are being mapped to by fields
|
||||
|
|
|
@ -14,7 +14,7 @@ from collections import OrderedDict
|
|||
import pytest
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.validators import (
|
||||
MaxValueValidator, MinLengthValidator, MinValueValidator
|
||||
MaxValueValidator, MinLengthValidator, MinValueValidator, EmailValidator, URLValidator
|
||||
)
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
|
@ -286,6 +286,34 @@ class TestRegularFieldMappings(TestCase):
|
|||
""")
|
||||
self.assertEqual(repr(TestSerializer()), expected)
|
||||
|
||||
def test_extra_field_validators(self):
|
||||
"""
|
||||
Ensure `extra_validators` are included to generated fields.
|
||||
"""
|
||||
class TestModel(models.Model):
|
||||
int = models.IntegerField()
|
||||
email = models.CharField(unique=True)
|
||||
url = models.CharField(validators=[URLValidator()])
|
||||
avatar = models.CharField()
|
||||
|
||||
class TestSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TestModel
|
||||
fields = ('int', 'email', 'url', 'avatar')
|
||||
extra_validators = {
|
||||
'email': [EmailValidator()],
|
||||
'avatar': [URLValidator()],
|
||||
}
|
||||
|
||||
expected = dedent("""
|
||||
TestSerializer():
|
||||
int = IntegerField()
|
||||
email = CharField(validators=[<django.core.validators.MaxLengthValidator object>, <UniqueValidator(queryset=TestModel.objects.all())>, <django.core.validators.EmailValidator object>])
|
||||
url = CharField(validators=[<django.core.validators.URLValidator object>, <django.core.validators.MaxLengthValidator object>])
|
||||
avatar = CharField(validators=[<django.core.validators.MaxLengthValidator object>, <django.core.validators.URLValidator object>])
|
||||
""")
|
||||
self.assertEqual(repr(TestSerializer()), expected)
|
||||
|
||||
def test_invalid_field(self):
|
||||
"""
|
||||
Field names that do not map to a model field or relationship should
|
||||
|
|
Loading…
Reference in New Issue
Block a user