Reduce indentation in BaseSerializer.errors

This makes the method more linear and easier to read.
This commit is contained in:
Ian Foote 2014-03-27 15:20:13 +00:00
parent 2a27674a79
commit f64599c361

View File

@ -493,7 +493,9 @@ class BaseSerializer(WritableField):
Run deserialization and return error data,
setting self.object if no errors occurred.
"""
if self._errors is None:
if self._errors is not None:
return self._errors
data, files = self.init_data, self.init_files
if self.many is not None:
@ -505,7 +507,14 @@ class BaseSerializer(WritableField):
'Use the `many=True` flag when instantiating the serializer.',
DeprecationWarning, stacklevel=3)
if many:
if not many:
ret = self.from_native(data, files)
if not self._errors:
self.object = ret
return self._errors
ret = RelationsList()
errors = []
update = self.object is not None
@ -527,7 +536,8 @@ class BaseSerializer(WritableField):
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.']})
errors.append({'non_field_errors': [
'Cannot create a new item, only existing items may be updated.']})
continue
ret.append(self.from_native(item, None))
@ -539,8 +549,6 @@ class BaseSerializer(WritableField):
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:
self.object = ret