mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
commit
cc39b9c925
|
@ -727,6 +727,22 @@ class ModelSerializer(Serializer):
|
|||
kwargs['choices'] = model_field.flatchoices
|
||||
return ChoiceField(**kwargs)
|
||||
|
||||
attribute_dict = {
|
||||
models.CharField: ['max_length'],
|
||||
models.CommaSeparatedIntegerField: ['max_length'],
|
||||
models.DecimalField: ['max_digits', 'decimal_places'],
|
||||
models.EmailField: ['max_length'],
|
||||
models.FileField: ['max_length'],
|
||||
models.ImageField: ['max_length'],
|
||||
models.SlugField: ['max_length'],
|
||||
models.URLField: ['max_length'],
|
||||
}
|
||||
|
||||
if model_field.__class__ in attribute_dict:
|
||||
attributes = attribute_dict[model_field.__class__]
|
||||
for attribute in attributes:
|
||||
kwargs.update({attribute: getattr(model_field, attribute)})
|
||||
|
||||
try:
|
||||
return self.field_mapping[model_field.__class__](**kwargs)
|
||||
except KeyError:
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.test import TestCase
|
|||
from rest_framework import serializers
|
||||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel)
|
||||
import datetime
|
||||
import pickle
|
||||
|
||||
|
@ -1321,3 +1321,84 @@ class DeserializeListTestCase(TestCase):
|
|||
self.assertFalse(serializer.is_valid())
|
||||
expected = [{}, {'email': ['This field is required.']}, {}]
|
||||
self.assertEqual(serializer.errors, expected)
|
||||
|
||||
|
||||
class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
class AMOAFModel(RESTFrameworkModel):
|
||||
char_field = models.CharField(max_length=1024, blank=True)
|
||||
comma_separated_integer_field = models.CommaSeparatedIntegerField(max_length=1024, blank=True)
|
||||
decimal_field = models.DecimalField(max_digits=64, decimal_places=32, blank=True)
|
||||
email_field = models.EmailField(max_length=1024, blank=True)
|
||||
file_field = models.FileField(max_length=1024, blank=True)
|
||||
image_field = models.ImageField(max_length=1024, blank=True)
|
||||
slug_field = models.SlugField(max_length=1024, blank=True)
|
||||
url_field = models.URLField(max_length=1024, blank=True)
|
||||
|
||||
class AMOAFSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = AMOAFModel
|
||||
|
||||
self.serializer_class = AMOAFSerializer
|
||||
self.fields_attributes = {
|
||||
'char_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'comma_separated_integer_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'decimal_field': [
|
||||
('max_digits', 64),
|
||||
('decimal_places', 32),
|
||||
],
|
||||
'email_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'file_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'image_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'slug_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
'url_field': [
|
||||
('max_length', 1024),
|
||||
],
|
||||
}
|
||||
|
||||
def field_test(self, field):
|
||||
serializer = self.serializer_class(data={})
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
|
||||
for attribute in self.fields_attributes[field]:
|
||||
self.assertEqual(
|
||||
getattr(serializer.fields[field], attribute[0]),
|
||||
attribute[1]
|
||||
)
|
||||
|
||||
def test_char_field(self):
|
||||
self.field_test('char_field')
|
||||
|
||||
def test_comma_separated_integer_field(self):
|
||||
self.field_test('comma_separated_integer_field')
|
||||
|
||||
def test_decimal_field(self):
|
||||
self.field_test('decimal_field')
|
||||
|
||||
def test_email_field(self):
|
||||
self.field_test('email_field')
|
||||
|
||||
def test_file_field(self):
|
||||
self.field_test('file_field')
|
||||
|
||||
def test_image_field(self):
|
||||
self.field_test('image_field')
|
||||
|
||||
def test_slug_field(self):
|
||||
self.field_test('slug_field')
|
||||
|
||||
def test_url_field(self):
|
||||
self.field_test('url_field')
|
||||
|
|
Loading…
Reference in New Issue
Block a user