mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-23 22:49:50 +03:00
added support for partial activation over serializer meta option
This commit is contained in:
parent
628e3bf001
commit
ddcc5766e1
|
@ -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_type>_field` methods.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {}
|
||||
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user