Ensure read-only fields don't break with current HTML renderer behavior

This commit is contained in:
Tom Christie 2013-10-10 17:33:22 +01:00
parent 8d4ba478cc
commit 9e29c63895
4 changed files with 21 additions and 2 deletions

View File

@ -777,6 +777,7 @@ class IntegerField(WritableField):
type_name = 'IntegerField'
type_label = 'integer'
form_field_class = forms.IntegerField
empty = 0
default_error_messages = {
'invalid': _('Enter a whole number.'),
@ -808,6 +809,7 @@ class FloatField(WritableField):
type_name = 'FloatField'
type_label = 'float'
form_field_class = forms.FloatField
empty = 0
default_error_messages = {
'invalid': _("'%s' value must be a float."),
@ -828,6 +830,7 @@ class DecimalField(WritableField):
type_name = 'DecimalField'
type_label = 'decimal'
form_field_class = forms.DecimalField
empty = Decimal('0')
default_error_messages = {
'invalid': _('Enter a number.'),

View File

@ -310,6 +310,8 @@ class BaseSerializer(WritableField):
ret.fields = self._dict_class()
for field_name, field in self.fields.items():
if field.read_only and obj is None:
continue
field.initialize(parent=self, field_name=field_name)
key = self.get_field_key(field_name)
value = field.field_to_native(obj, field_name)

View File

@ -159,8 +159,7 @@ class BasicTests(TestCase):
expected = {
'email': '',
'content': '',
'created': None,
'sub_comment': ''
'created': None
}
self.assertEqual(serializer.data, expected)

View File

@ -0,0 +1,15 @@
from django.test import TestCase
from rest_framework import serializers
class EmptySerializerTestCase(TestCase):
def test_empty_serializer(self):
class FooBarSerializer(serializers.Serializer):
foo = serializers.IntegerField()
bar = serializers.SerializerMethodField('get_bar')
def get_bar(self, obj):
return 'bar'
serializer = FooBarSerializer()
self.assertEquals(serializer.data, {'foo': 0})