mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 20:10:10 +03:00
Merge c0c8833185
into 511e874185
This commit is contained in:
commit
ab6b4ab521
|
@ -495,57 +495,71 @@ class BaseSerializer(WritableField):
|
||||||
Run deserialization and return error data,
|
Run deserialization and return error data,
|
||||||
setting self.object if no errors occurred.
|
setting self.object if no errors occurred.
|
||||||
"""
|
"""
|
||||||
if self._errors is None:
|
if self._errors is not None:
|
||||||
data, files = self.init_data, self.init_files
|
return self._errors
|
||||||
|
|
||||||
if self.many is not None:
|
data, files = self.init_data, self.init_files
|
||||||
many = self.many
|
|
||||||
else:
|
|
||||||
many = hasattr(data, '__iter__') and not isinstance(data, (Page, dict, six.text_type))
|
|
||||||
if many:
|
|
||||||
warnings.warn('Implicit list/queryset serialization is deprecated. '
|
|
||||||
'Use the `many=True` flag when instantiating the serializer.',
|
|
||||||
DeprecationWarning, stacklevel=3)
|
|
||||||
|
|
||||||
|
if self.many is not None:
|
||||||
|
many = self.many
|
||||||
|
else:
|
||||||
|
many = hasattr(data, '__iter__') and not isinstance(data, (Page, dict, six.text_type))
|
||||||
if many:
|
if many:
|
||||||
ret = RelationsList()
|
warnings.warn('Implicit list/queryset serialization is deprecated. '
|
||||||
errors = []
|
'Use the `many=True` flag when instantiating the serializer.',
|
||||||
update = self.object is not None
|
DeprecationWarning, stacklevel=3)
|
||||||
|
|
||||||
if update:
|
if many:
|
||||||
# If this is a bulk update we need to map all the objects
|
return self._errors_many(data)
|
||||||
# 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)):
|
ret = self.from_native(data, files)
|
||||||
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)
|
|
||||||
if self.object is None and not self.allow_add_remove:
|
|
||||||
ret.append(None)
|
|
||||||
errors.append({'non_field_errors': ['Cannot create a new item, only existing items may be updated.']})
|
|
||||||
continue
|
|
||||||
|
|
||||||
ret.append(self.from_native(item, None))
|
if not self._errors:
|
||||||
errors.append(self._errors)
|
self.object = ret
|
||||||
|
|
||||||
if update and self.allow_add_remove:
|
return self._errors
|
||||||
ret._deleted = identity_to_objects.values()
|
|
||||||
|
|
||||||
self._errors = any(errors) and errors or []
|
def _errors_many(self, data):
|
||||||
else:
|
"""Run deserialization of bulk data."""
|
||||||
self._errors = {'non_field_errors': ['Expected a list of items.']}
|
|
||||||
else:
|
|
||||||
ret = self.from_native(data, files)
|
|
||||||
|
|
||||||
if not self._errors:
|
if not hasattr(data, '__iter__') or isinstance(data, (dict, six.text_type)):
|
||||||
self.object = ret
|
self._errors = {'non_field_errors': ['Expected a list of items.']}
|
||||||
|
return self._errors
|
||||||
|
|
||||||
|
ret = RelationsList()
|
||||||
|
errors = []
|
||||||
|
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))
|
||||||
|
|
||||||
|
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)
|
||||||
|
if self.object is None and not self.allow_add_remove:
|
||||||
|
ret.append(None)
|
||||||
|
errors.append({'non_field_errors': [
|
||||||
|
'Cannot create a new item, only existing items may be updated.']})
|
||||||
|
continue
|
||||||
|
|
||||||
|
ret.append(self.from_native(item, None))
|
||||||
|
errors.append(self._errors)
|
||||||
|
|
||||||
|
if update and self.allow_add_remove:
|
||||||
|
ret._deleted = identity_to_objects.values()
|
||||||
|
|
||||||
|
self._errors = any(errors) and errors or []
|
||||||
|
|
||||||
|
if not self._errors:
|
||||||
|
self.object = ret
|
||||||
|
|
||||||
return self._errors
|
return self._errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user