【发布时间】:2018-11-07 14:25:43
【问题描述】:
我的 SQLAlchemy 表中有一个嵌套对象,由 Marshmallow's nested schema feature 生成。例如,articles 对象 GET 响应将包含一个 author(User 类型)对象。
我知道JSONAPI spec 已经允许更新关系。但是,通常我想在一次调用中更新一篇文章以及它的嵌套对象(包含新作者的文章的 POST 请求将自动创建作者)。是否可以发出包含尚不存在的关系对象资源的 PATCH 请求?
所以不只是这样:
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1" }
}
}
}
}
如果不存在作者,最好将其传入以创建新作者(这不是我的实际用例,但我有类似的现实生活需求):
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1", "attributes": {"name": "new author":, "articles_written": 1} }
}
}
}
}
这完全可能吗?或者对哪些 REST 框架可以支持这一点有建议,还是完全违反 JSON API 规范?
【问题讨论】:
标签: flask json-api marshmallow flask-restless jsonapiframework