用棉花糖格式化json api 1.0(https://jsonapi.org)
marshmallow-jsonapi的Python项目详细描述
主页:http://marshmallow-jsonapi.readthedocs.io/
json api 1.0(https://jsonapi.org)使用marshmallow格式化。
marshmallow json api提供了一种在任何python web框架中生成jsonapi兼容数据的简单方法。
frommarshmallow_jsonapiimportSchema,fieldsclassPostSchema(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 linkagemany=True,include_resource_linkage=True,type_="comments",)classMeta:type_="posts"strict=Truepost_schema=PostSchema()post_schema.dump(post).data# {# "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
文档
要求
- python>;=2.7或>;=3.5
项目链接
许可证
麻省理工学院许可。有关详细信息,请参见捆绑的LICENSE文件。