def _deserialize(self, value, attr, data):
embedded_document_cls = self.embedded_document_cls
if isinstance(value, embedded_document_cls):
return value
# Handle inheritance deserialization here using `cls` field as hint
if embedded_document_cls.opts.offspring and isinstance(value, dict) and 'cls' in value:
to_use_cls_name = value.pop('cls')
if not any(o for o in embedded_document_cls.opts.offspring
if o.__name__ == to_use_cls_name):
raise ValidationError(_('Unknown document `{document}`.').format(
document=to_use_cls_name))
try:
to_use_cls = embedded_document_cls.opts.instance.retrieve_embedded_document(
to_use_cls_name)
except NotRegisteredDocumentError as e:
raise ValidationError(str(e))
return to_use_cls(**value)
else:
# `Nested._deserialize` calls schema.load without partial=True
data, errors = self.schema.load(value, partial=True)
if errors:
raise ValidationError(errors, data=data)
return self._deserialize_from_mongo(data)
评论列表
文章目录