mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Ensure read-only fields don't break with current HTML renderer behavior
This commit is contained in:
parent
8d4ba478cc
commit
9e29c63895
|
@ -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.'),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -159,8 +159,7 @@ class BasicTests(TestCase):
|
|||
expected = {
|
||||
'email': '',
|
||||
'content': '',
|
||||
'created': None,
|
||||
'sub_comment': ''
|
||||
'created': None
|
||||
}
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
|
|
15
rest_framework/tests/test_serializer_empty.py
Normal file
15
rest_framework/tests/test_serializer_empty.py
Normal 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})
|
Loading…
Reference in New Issue
Block a user