mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Made field mapping use mix/max kwargs for DurationField validators.
This commit is contained in:
parent
a4c9e4c911
commit
d1822ec6b6
|
@ -12,7 +12,7 @@ from rest_framework.compat import postgres_fields
|
||||||
from rest_framework.validators import UniqueValidator
|
from rest_framework.validators import UniqueValidator
|
||||||
|
|
||||||
NUMERIC_FIELD_TYPES = (
|
NUMERIC_FIELD_TYPES = (
|
||||||
models.IntegerField, models.FloatField, models.DecimalField
|
models.IntegerField, models.FloatField, models.DecimalField, models.DurationField,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ an appropriate set of serializer fields for each case.
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
@ -16,7 +17,6 @@ from django.core.validators import (
|
||||||
MaxValueValidator, MinLengthValidator, MinValueValidator
|
MaxValueValidator, MinLengthValidator, MinValueValidator
|
||||||
)
|
)
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import DurationField as ModelDurationField
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
|
@ -349,7 +349,7 @@ class TestDurationFieldMapping(TestCase):
|
||||||
"""
|
"""
|
||||||
A model that defines DurationField.
|
A model that defines DurationField.
|
||||||
"""
|
"""
|
||||||
duration_field = ModelDurationField()
|
duration_field = models.DurationField()
|
||||||
|
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -363,6 +363,27 @@ class TestDurationFieldMapping(TestCase):
|
||||||
""")
|
""")
|
||||||
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
||||||
|
|
||||||
|
def test_duration_field_with_validators(self):
|
||||||
|
class ValidatedDurationFieldModel(models.Model):
|
||||||
|
"""
|
||||||
|
A model that defines DurationField with validators.
|
||||||
|
"""
|
||||||
|
duration_field = models.DurationField(
|
||||||
|
validators=[MinValueValidator(datetime.timedelta(days=1)), MaxValueValidator(datetime.timedelta(days=3))]
|
||||||
|
)
|
||||||
|
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ValidatedDurationFieldModel
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
expected = dedent("""
|
||||||
|
TestSerializer():
|
||||||
|
id = IntegerField(label='ID', read_only=True)
|
||||||
|
duration_field = DurationField(max_value=datetime.timedelta(3), min_value=datetime.timedelta(1))
|
||||||
|
""")
|
||||||
|
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
||||||
|
|
||||||
|
|
||||||
class TestGenericIPAddressFieldValidation(TestCase):
|
class TestGenericIPAddressFieldValidation(TestCase):
|
||||||
def test_ip_address_validation(self):
|
def test_ip_address_validation(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user