mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Add support for partial serializer updates
This commit is contained in:
parent
d22daa04e6
commit
c3644234cd
|
@ -148,7 +148,7 @@ class WritableField(Field):
|
|||
self.widget = widget
|
||||
|
||||
def validate(self, value):
|
||||
if value in validators.EMPTY_VALUES and self.required:
|
||||
if value in validators.EMPTY_VALUES and self.required and not self.root.partial:
|
||||
raise ValidationError(self.error_messages['required'])
|
||||
|
||||
def run_validators(self, value):
|
||||
|
@ -186,7 +186,7 @@ class WritableField(Field):
|
|||
if self.default is not None:
|
||||
native = self.default
|
||||
else:
|
||||
if self.required:
|
||||
if self.required and not self.root.partial:
|
||||
raise ValidationError(self.error_messages['required'])
|
||||
return
|
||||
|
||||
|
|
|
@ -91,12 +91,13 @@ class BaseSerializer(Field):
|
|||
_options_class = SerializerOptions
|
||||
_dict_class = SortedDictWithMetadata # Set to unsorted dict for backwards compatibility with unsorted implementations.
|
||||
|
||||
def __init__(self, instance=None, data=None, files=None, context=None, **kwargs):
|
||||
def __init__(self, instance=None, data=None, files=None, context=None, partial=False, **kwargs):
|
||||
super(BaseSerializer, self).__init__(**kwargs)
|
||||
self.opts = self._options_class(self.Meta)
|
||||
self.fields = copy.deepcopy(self.base_fields)
|
||||
self.parent = None
|
||||
self.root = None
|
||||
self.partial = partial
|
||||
|
||||
self.context = context or {}
|
||||
|
||||
|
|
|
@ -108,6 +108,18 @@ class BasicTests(TestCase):
|
|||
self.assertTrue(serializer.object is expected)
|
||||
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
|
||||
|
||||
def test_partial_update(self):
|
||||
msg = 'Merry New Year!'
|
||||
partial_data = {'content': msg}
|
||||
serializer = CommentSerializer(self.comment, data=partial_data)
|
||||
self.assertEquals(serializer.is_valid(), False)
|
||||
serializer = CommentSerializer(self.comment, data=partial_data, partial=True)
|
||||
expected = self.comment
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEquals(serializer.object, expected)
|
||||
self.assertTrue(serializer.object is expected)
|
||||
self.assertEquals(serializer.data['content'], msg)
|
||||
|
||||
def test_model_fields_as_expected(self):
|
||||
"""
|
||||
Make sure that the fields returned are the same as defined
|
||||
|
|
Loading…
Reference in New Issue
Block a user