跳转到主要内容

使用 marshmallow 格式化 JSON API 1.0 (https://jsonapi.org)

项目描述

PyPI version Build status Documentation marshmallow 3 compatible code style: black

首页: http://marshmallow-jsonapi.readthedocs.io/

JSON API 1.0 (https://jsonapi.org) 使用 marshmallow 格式化。

marshmallow-jsonapi 为任何 Python Web 框架提供了一个简单的方式来生成符合 JSON API 规范的数据。

from marshmallow_jsonapi import Schema, fields


class PostSchema(Schema):
    id = fields.Str(dump_only=True)
    title = fields.Str()

    author = fields.Relationship(
        "/authors/{author_id}", related_url_kwargs={"author_id": "<author.id>"}
    )

    comments = fields.Relationship(
        "/posts/{post_id}/comments",
        related_url_kwargs={"post_id": "<id>"},
        # Include resource linkage
        many=True,
        include_resource_linkage=True,
        type_="comments",
    )

    class Meta:
        type_ = "posts"


post_schema = PostSchema()
post_schema.dump(post)
# {
#     "data": {
#         "id": "1",
#         "type": "posts"
#         "attributes": {
#             "title": "JSON API paints my bikeshed!"
#         },
#         "relationships": {
#             "author": {
#                 "links": {
#                     "related": "/authors/9"
#                 }
#             },
#             "comments": {
#                 "links": {
#                     "related": "/posts/1/comments/"
#                 }
#                 "data": [
#                     {"id": 5, "type": "comments"},
#                     {"id": 12, "type": "comments"}
#                 ],
#             }
#         },
#     }
# }

安装

pip install marshmallow-jsonapi

文档

完整的文档可以在 https://marshmallow-jsonapi.readthedocs.io/ 找到。

要求

  • Python >= 3.6

许可证

MIT 许可。有关更多详细信息,请参阅附带 LICENSE 文件。

项目详情


下载文件

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

源分发

marshmallow-jsonapi-0.24.0.tar.gz (34.6 kB 查看哈希值)

上传时间

构建分发

marshmallow_jsonapi-0.24.0-py2.py3-none-any.whl (14.3 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面