mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 21:10:13 +03:00
Reduce indentation in BaseSerializer.errors
This makes the method more linear and easier to read.
This commit is contained in:
parent
2a27674a79
commit
f64599c361
|
@ -493,58 +493,66 @@ 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 not many:
|
||||||
# If this is a bulk update we need to map all the objects
|
ret = self.from_native(data, files)
|
||||||
# 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)
|
|
||||||
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 []
|
|
||||||
else:
|
|
||||||
self._errors = {'non_field_errors': ['Expected a list of items.']}
|
|
||||||
else:
|
|
||||||
ret = self.from_native(data, files)
|
|
||||||
|
|
||||||
if not self._errors:
|
if not self._errors:
|
||||||
self.object = ret
|
self.object = ret
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
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)
|
||||||
|
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 []
|
||||||
|
else:
|
||||||
|
self._errors = {'non_field_errors': ['Expected a list of items.']}
|
||||||
|
|
||||||
|
if not self._errors:
|
||||||
|
self.object = ret
|
||||||
|
|
||||||
return self._errors
|
return self._errors
|
||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
|
@ -757,9 +765,9 @@ class ModelSerializer(Serializer):
|
||||||
field.read_only = True
|
field.read_only = True
|
||||||
|
|
||||||
ret[accessor_name] = field
|
ret[accessor_name] = field
|
||||||
|
|
||||||
# Ensure that 'read_only_fields' is an iterable
|
# Ensure that 'read_only_fields' is an iterable
|
||||||
assert isinstance(self.opts.read_only_fields, (list, tuple)), '`read_only_fields` must be a list or tuple'
|
assert isinstance(self.opts.read_only_fields, (list, tuple)), '`read_only_fields` must be a list or tuple'
|
||||||
|
|
||||||
# Add the `read_only` flag to any fields that have been specified
|
# Add the `read_only` flag to any fields that have been specified
|
||||||
# in the `read_only_fields` option
|
# in the `read_only_fields` option
|
||||||
|
@ -774,10 +782,10 @@ class ModelSerializer(Serializer):
|
||||||
"on serializer '%s'." %
|
"on serializer '%s'." %
|
||||||
(field_name, self.__class__.__name__))
|
(field_name, self.__class__.__name__))
|
||||||
ret[field_name].read_only = True
|
ret[field_name].read_only = True
|
||||||
|
|
||||||
# Ensure that 'write_only_fields' is an iterable
|
# Ensure that 'write_only_fields' is an iterable
|
||||||
assert isinstance(self.opts.write_only_fields, (list, tuple)), '`write_only_fields` must be a list or tuple'
|
assert isinstance(self.opts.write_only_fields, (list, tuple)), '`write_only_fields` must be a list or tuple'
|
||||||
|
|
||||||
for field_name in self.opts.write_only_fields:
|
for field_name in self.opts.write_only_fields:
|
||||||
assert field_name not in self.base_fields.keys(), (
|
assert field_name not in self.base_fields.keys(), (
|
||||||
"field '%s' on serializer '%s' specified in "
|
"field '%s' on serializer '%s' specified in "
|
||||||
|
@ -788,7 +796,7 @@ class ModelSerializer(Serializer):
|
||||||
"Non-existant field '%s' specified in `write_only_fields` "
|
"Non-existant field '%s' specified in `write_only_fields` "
|
||||||
"on serializer '%s'." %
|
"on serializer '%s'." %
|
||||||
(field_name, self.__class__.__name__))
|
(field_name, self.__class__.__name__))
|
||||||
ret[field_name].write_only = True
|
ret[field_name].write_only = True
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user