mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 21:14:01 +03:00
Merge pull request #422 from markotibold/max_length_for_modelserializers
Max length for modelserializers
This commit is contained in:
commit
e801e21210
|
@ -131,6 +131,18 @@ or `django.db.models.fields.TextField`.
|
|||
|
||||
**Signature:** `CharField(max_length=None, min_length=None)`
|
||||
|
||||
## URLField
|
||||
|
||||
Corresponds to `django.db.models.fields.URLField`. Uses Django's `django.core.validators.URLValidator` for validation.
|
||||
|
||||
**Signature:** `CharField(max_length=200, min_length=None)`
|
||||
|
||||
## SlugField
|
||||
|
||||
Corresponds to `django.db.models.fields.SlugField`.
|
||||
|
||||
**Signature:** `CharField(max_length=50, min_length=None)`
|
||||
|
||||
## ChoiceField
|
||||
|
||||
A field that can accept a value out of a limited set of choices.
|
||||
|
|
|
@ -706,6 +706,23 @@ class CharField(WritableField):
|
|||
return smart_unicode(value)
|
||||
|
||||
|
||||
class URLField(CharField):
|
||||
type_name = 'URLField'
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
kwargs['max_length'] = kwargs.get('max_length', 200)
|
||||
kwargs['validators'] = [validators.URLValidator()]
|
||||
super(URLField, self).__init__(**kwargs)
|
||||
|
||||
|
||||
class SlugField(CharField):
|
||||
type_name = 'SlugField'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['max_length'] = kwargs.get('max_length', 50)
|
||||
super(SlugField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ChoiceField(WritableField):
|
||||
type_name = 'ChoiceField'
|
||||
widget = widgets.Select
|
||||
|
|
|
@ -429,6 +429,10 @@ class ModelSerializer(Serializer):
|
|||
kwargs['choices'] = model_field.flatchoices
|
||||
return ChoiceField(**kwargs)
|
||||
|
||||
max_length = getattr(model_field, 'max_length', None)
|
||||
if max_length:
|
||||
kwargs['max_length'] = max_length
|
||||
|
||||
field_mapping = {
|
||||
models.FloatField: FloatField,
|
||||
models.IntegerField: IntegerField,
|
||||
|
@ -439,6 +443,8 @@ class ModelSerializer(Serializer):
|
|||
models.DateField: DateField,
|
||||
models.EmailField: EmailField,
|
||||
models.CharField: CharField,
|
||||
models.URLField: URLField,
|
||||
models.SlugField: SlugField,
|
||||
models.TextField: CharField,
|
||||
models.CommaSeparatedIntegerField: CharField,
|
||||
models.BooleanField: BooleanField,
|
||||
|
|
|
@ -239,6 +239,14 @@ class ValidationTests(TestCase):
|
|||
self.assertEquals(serializer.is_valid(), True)
|
||||
self.assertEquals(serializer.errors, {})
|
||||
|
||||
def test_modelserializer_max_length_exceeded(self):
|
||||
data = {
|
||||
'title': 'x' * 201,
|
||||
}
|
||||
serializer = ActionItemSerializer(data=data)
|
||||
self.assertEquals(serializer.is_valid(), False)
|
||||
self.assertEquals(serializer.errors, {'title': [u'Ensure this value has at most 200 characters (it has 201).']})
|
||||
|
||||
|
||||
class MetadataTests(TestCase):
|
||||
def test_empty(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user