serializers.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:esdc-ce 作者: erigones 项目源码 文件源码
def errors(self):
        """
        Run deserialization and return error data,
        setting self.object if no errors occurred.
        """
        if self._errors is None:
            data, files = self.init_data, self.init_files

            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:
                    raise AssertionError('Implicit list/queryset serialization is deprecated. '
                                         'Use the `many=True` flag when instantiating the serializer.')

            if many:
                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)
                            # noinspection PyUnboundLocalVariable
                            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:
                self.object = ret

        return self._errors
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号