跳转到主要内容

Mongoengine与marshmallow(反)序列化库的集成

项目描述

Travis-CI Documentation Status Code Coverage

marshmallow-mongoengine

Mongoenginemarshmallow(反)序列化库的集成。

请参阅文档:http://marshmallow-mongoengine.rtfd.org/

声明您的模型

import mongoengine as me

class Author(me.Document):
    id = me.IntField(primary_key=True, default=1)
    name = me.StringField()
    books = me.ListField(me.ReferenceField('Book'))

    def __repr__(self):
        return '<Author(name={self.name!r})>'.format(self=self)


class Book(me.Document):
    title = me.StringField()

生成marshmallow模式

from marshmallow_mongoengine import ModelSchema

class AuthorSchema(ModelSchema):
    class Meta:
        model = Author

class BookSchema(ModelSchema):
    class Meta:
        model = Book

author_schema = AuthorSchema()

序列化和反序列化您的数据

author = Author(name='Chuck Paluhniuk').save()
book = Book(title='Fight Club', author=author).save()

dump_data = author_schema.dump(author).data
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': ['5578726b7a58012298a5a7e2']}

author_schema.load(dump_data).data
# <Author(name='Chuck Paluhniuk')>

立即获取

pip install -U marshmallow-mongoengine

许可证

MIT许可。有关详细信息,请参阅捆绑的LICENSE文件。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。

源代码分发

marshmallow-mongoengine-0.31.2.tar.gz (11.4 kB 查看哈希值)

上传时间 源代码

构建分发

marshmallow_mongoengine-0.31.2-py2.py3-none-any.whl (12.2 kB 查看哈希值)

上传时间 Python 2 Python 3