From ddcc5766e1bd40290f2be5c60775ee430961bddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Wed, 12 Dec 2012 13:02:00 +0100 Subject: [PATCH 1/2] added support for partial activation over serializer meta option --- docs/api-guide/serializers.md | 13 ++++++++++++- docs/topics/release-notes.md | 4 ++++ rest_framework/serializers.py | 5 +++-- rest_framework/tests/serializer.py | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 19efde3c7..6cd54a3da 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -254,13 +254,24 @@ The `depth` option should be set to an integer value that indicates the depth of ## Specifying which fields should be read-only -You may wish to specify multiple fields as read-only. Instead of adding each field explicitely with the `read_only=True` attribute, you may use the `read_only_fields` Meta option, like so: +You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the `read_only_fields` Meta option, like so: class AccountSerializer(serializers.ModelSerializer): class Meta: model = Account read_only_fields = ('created', 'modified') +## Specifying partial updates + +You may want to allow partial updates to your Model. To do so, you can set `partial = True` as a Meta option to your serializer: + + class AccountSerializer(serializers.ModelSerializer): + class Meta: + model = Account + partial = True + +By default `partial` will be `False`. + ## Customising the default fields You can create customized subclasses of `ModelSerializer` that use a different set of default fields for the representation, by overriding various `get__field` methods. diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index 4f83cfd8f..0c28e0d16 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -4,6 +4,10 @@ > > — Eric S. Raymond, [The Cathedral and the Bazaar][cite]. +## Master + +* Added `partial` to serializer meta options + ## 2.1.9 **Date**: 11th Dec 2012 diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5465d7b72..876072443 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -91,6 +91,7 @@ class SerializerOptions(object): self.depth = getattr(meta, 'depth', 0) self.fields = getattr(meta, 'fields', ()) self.exclude = getattr(meta, 'exclude', ()) + self.partial = getattr(meta, 'partial', False) class BaseSerializer(Field): @@ -100,12 +101,12 @@ 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, partial=False, **kwargs): + def __init__(self, instance=None, data=None, files=None, context=None, partial=None, **kwargs): super(BaseSerializer, self).__init__(**kwargs) self.opts = self._options_class(self.Meta) self.parent = None self.root = None - self.partial = partial + self.partial = partial or self.opts.partial self.context = context or {} diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 50a5f5a48..c2aefe691 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -41,6 +41,12 @@ class CommentSerializer(serializers.Serializer): return instance +class CommentPartialSerializer(CommentSerializer): + + class Meta: + partial = True + + class BookSerializer(serializers.ModelSerializer): isbn = serializers.RegexField(regex=r'^[0-9]{13}$', error_messages={'invalid': 'isbn has to be exact 13 numbers'}) @@ -134,6 +140,14 @@ class BasicTests(TestCase): self.assertEquals(serializer.object, expected) self.assertTrue(serializer.object is expected) self.assertEquals(serializer.data['content'], msg) + msg = 'Merry New Year wishes for 2013!' + partial_data = {'content': msg} + serializer = CommentPartialSerializer(self.comment, data=partial_data) + 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): """ From 7617b673e780e19a4b1f99e84e5d031b6c2ef2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Wed, 12 Dec 2012 15:21:49 +0100 Subject: [PATCH 2/2] fixed partial assigning --- rest_framework/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 876072443..35027f120 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -106,7 +106,7 @@ class BaseSerializer(Field): self.opts = self._options_class(self.Meta) self.parent = None self.root = None - self.partial = partial or self.opts.partial + self.partial = partial if partial is not None else self.opts.partial self.context = context or {}