aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/serializers.py
diff options
context:
space:
mode:
authorTom Christie2013-03-22 14:47:29 -0700
committerTom Christie2013-03-22 14:47:29 -0700
commit870d5c7d7810ecd7f187e13b5fe3a3bcba6b18c3 (patch)
treec3f45b1012eccccf52c9aedb890b37c690a438e2 /rest_framework/serializers.py
parent9a2ba4bf54d72452c2800bb3e51ea718a72250bc (diff)
parent13794baf7016f7d44daffb55d29e3dbc56f7612d (diff)
downloaddjango-rest-framework-870d5c7d7810ecd7f187e13b5fe3a3bcba6b18c3.tar.bz2
Merge pull request #744 from tomchristie/basic-bulk-edit
Basic bulk create and bulk update
Diffstat (limited to 'rest_framework/serializers.py')
-rw-r--r--rest_framework/serializers.py73
1 files changed, 68 insertions, 5 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index 4fe857a6..0b0c2c9c 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -110,13 +110,15 @@ class BaseSerializer(Field):
_dict_class = SortedDictWithMetadata
def __init__(self, instance=None, data=None, files=None,
- context=None, partial=False, many=None, source=None):
+ context=None, partial=False, many=None, source=None,
+ allow_delete=False):
super(BaseSerializer, self).__init__(source=source)
self.opts = self._options_class(self.Meta)
self.parent = None
self.root = None
self.partial = partial
self.many = many
+ self.allow_delete = allow_delete
self.context = context or {}
@@ -128,6 +130,13 @@ class BaseSerializer(Field):
self._data = None
self._files = None
self._errors = None
+ self._deleted = None
+
+ if many and instance is not None and not hasattr(instance, '__iter__'):
+ raise ValueError('instance should be a queryset or other iterable with many=True')
+
+ if allow_delete and not many:
+ raise ValueError('allow_delete should only be used for bulk updates, but you have not set many=True')
#####
# Methods to determine which fields to use when (de)serializing objects.
@@ -331,6 +340,20 @@ class BaseSerializer(Field):
return [self.to_native(item) for item in obj]
return self.to_native(obj)
+ def get_identity(self, data):
+ """
+ This hook is required for bulk update.
+ It is used to determine the canonical identity of a given object.
+
+ Note that the data has not been validated at this point, so we need
+ to make sure that we catch any cases of incorrect datatypes being
+ passed to this method.
+ """
+ try:
+ return data.get('id', None)
+ except AttributeError:
+ return None
+
@property
def errors(self):
"""
@@ -352,10 +375,33 @@ class BaseSerializer(Field):
if many:
ret = []
errors = []
- for item in data:
- ret.append(self.from_native(item, None))
- errors.append(self._errors)
- self._errors = any(errors) and errors or []
+ update = self.object is not None
+
+ if update:
+ # If this is a bulk update we need to map all the objects
+ # to a canonical identity so we can determine which
+ # individual object is being updated for each item in the
+ # incoming data
+ objects = self.object
+ identities = [self.get_identity(self.to_native(obj)) for obj in objects]
+ identity_to_objects = dict(zip(identities, objects))
+
+ if hasattr(data, '__iter__') and not isinstance(data, (dict, six.text_type)):
+ for item in data:
+ if update:
+ # Determine which object we're updating
+ identity = self.get_identity(item)
+ self.object = identity_to_objects.pop(identity, None)
+
+ ret.append(self.from_native(item, None))
+ errors.append(self._errors)
+
+ if update:
+ self._deleted = identity_to_objects.values()
+
+ self._errors = any(errors) and errors or []
+ else:
+ self._errors = {'non_field_errors': ['Expected a list of items']}
else:
ret = self.from_native(data, files)
@@ -394,6 +440,9 @@ class BaseSerializer(Field):
def save_object(self, obj, **kwargs):
obj.save(**kwargs)
+ def delete_object(self, obj):
+ obj.delete()
+
def save(self, **kwargs):
"""
Save the deserialized object and return it.
@@ -402,6 +451,10 @@ class BaseSerializer(Field):
[self.save_object(item, **kwargs) for item in self.object]
else:
self.save_object(self.object, **kwargs)
+
+ if self.allow_delete and self._deleted:
+ [self.delete_object(item) for item in self._deleted]
+
return self.object
@@ -690,3 +743,13 @@ class HyperlinkedModelSerializer(ModelSerializer):
'many': to_many
}
return HyperlinkedRelatedField(**kwargs)
+
+ def get_identity(self, data):
+ """
+ This hook is required for bulk update.
+ We need to override the default, to use the url as the identity.
+ """
+ try:
+ return data.get('url', None)
+ except AttributeError:
+ return None