使用 marshmallow 格式化 JSON API 1.0 (https://jsonapi.org)
项目描述
首页: 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 查看哈希值)
构建分发
关闭
hashes for marshmallow-jsonapi-0.24.0.tar.gz
算法 | 哈希摘要 | |
---|---|---|
SHA256 | bd88c0ac0e2ddeb0a3ceb86229963b9f828d898041f29d92a68f585a1feb37b5 |
|
MD5 | 14711955535ee9850ab13f89278a7a2f |
|
BLAKE2b-256 | 428e5148bdc8ffb44e03a259ac4c824d568ad9ce2653c63e216382ee94308bc3 |
关闭
hashes for marshmallow_jsonapi-0.24.0-py2.py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b7403688297dfe8b89173582811989badbe1328ac36447c5a151c006fbe34d24 |
|
MD5 | 2bb6b6293063e12cd7ddcc65176b78af |
|
BLAKE2b-256 | b6445cf50d083ac6e1ca654e1cbaab0342b1f231591a3cd184f72827eccfa9f3 |