mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Added model form field -> serializer form field mapping
This commit is contained in:
parent
f010a9553e
commit
d1b99f350a
|
@ -439,19 +439,3 @@ class FloatField(Field):
|
|||
except (TypeError, ValueError):
|
||||
msg = self.error_messages['invalid'] % value
|
||||
raise ValidationError(msg)
|
||||
|
||||
# field_mapping = {
|
||||
# models.AutoField: IntegerField,
|
||||
# models.BooleanField: BooleanField,
|
||||
# models.CharField: CharField,
|
||||
# models.DateTimeField: DateTimeField,
|
||||
# models.DateField: DateField,
|
||||
# models.BigIntegerField: IntegerField,
|
||||
# models.IntegerField: IntegerField,
|
||||
# models.PositiveIntegerField: IntegerField,
|
||||
# models.FloatField: FloatField
|
||||
# }
|
||||
|
||||
|
||||
# def modelfield_to_serializerfield(field):
|
||||
# return field_mapping.get(type(field), Field)
|
||||
|
|
|
@ -3,6 +3,7 @@ import datetime
|
|||
import types
|
||||
from decimal import Decimal
|
||||
from django.core.serializers.base import DeserializedObject
|
||||
from django.db import models
|
||||
from django.utils.datastructures import SortedDict
|
||||
from rest_framework.compat import get_concrete_model
|
||||
from rest_framework.fields import *
|
||||
|
@ -349,7 +350,20 @@ class ModelSerializer(RelatedField, Serializer):
|
|||
"""
|
||||
Creates a default instance of a basic non-relational field.
|
||||
"""
|
||||
return Field()
|
||||
field_mapping = dict([
|
||||
[models.FloatField, FloatField],
|
||||
[models.IntegerField, IntegerField],
|
||||
[models.DateTimeField, DateTimeField],
|
||||
[models.DateField, DateField],
|
||||
[models.EmailField, EmailField],
|
||||
[models.CharField, CharField],
|
||||
[models.CommaSeparatedIntegerField, CharField],
|
||||
[models.BooleanField, BooleanField]
|
||||
])
|
||||
try:
|
||||
return field_mapping[model_field.__class__]()
|
||||
except KeyError:
|
||||
return Field()
|
||||
|
||||
def restore_object(self, attrs, instance=None):
|
||||
"""
|
||||
|
|
|
@ -52,7 +52,8 @@ class TestRootView(TestCase):
|
|||
POST requests to RootAPIView should create a new object.
|
||||
"""
|
||||
content = {'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content), content_type='application/json')
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEquals(response.data, {'id': 4, 'text': u'foobar'})
|
||||
|
@ -64,7 +65,8 @@ class TestRootView(TestCase):
|
|||
PUT requests to RootAPIView should not be allowed
|
||||
"""
|
||||
content = {'text': 'foobar'}
|
||||
request = factory.put('/', json.dumps(content), content_type='application/json')
|
||||
request = factory.put('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
self.assertEquals(response.data, {"detail": "Method 'PUT' not allowed."})
|
||||
|
@ -105,7 +107,8 @@ class TestRootView(TestCase):
|
|||
POST requests to create a new object should not be able to set the id.
|
||||
"""
|
||||
content = {'id': 999, 'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content), content_type='application/json')
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEquals(response.data, {'id': 4, 'text': u'foobar'})
|
||||
|
@ -142,7 +145,8 @@ class TestInstanceView(TestCase):
|
|||
POST requests to InstanceAPIView should not be allowed
|
||||
"""
|
||||
content = {'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content), content_type='application/json')
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
self.assertEquals(response.data, {"detail": "Method 'POST' not allowed."})
|
||||
|
@ -152,7 +156,8 @@ class TestInstanceView(TestCase):
|
|||
PUT requests to InstanceAPIView should update an object.
|
||||
"""
|
||||
content = {'text': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content), content_type='application/json')
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
@ -197,7 +202,8 @@ class TestInstanceView(TestCase):
|
|||
POST requests to create a new object should not be able to set the id.
|
||||
"""
|
||||
content = {'id': 999, 'text': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content), content_type='application/json')
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
|
Loading…
Reference in New Issue
Block a user