【发布时间】:2019-07-18 10:48:19
【问题描述】:
我是第一次编写 Python Flask WebApp。使用 Flask、SQLAlchemy、Marshmallow 作为我的主要包。我有一个嵌套模式,但在父页面中我没有显示子页面,但我想将所有子 ID 带入父页面,以便在详细信息页面中加载所有子页面。我修剪了孩子们只返回 ids,但是我不希望它们作为一个属性对象,而是想要 ids 数组。
我如何像这样更改 JSON,
{
"description": "Report Name",
"id": 1,
"load_date_time": "2019-02-12T05:14:28+00:00",
"children": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"publish_date_time": "2018-09-03T00:00:00+00:00",
"summary": "Summary will also be present. Usually two to three brief sentences about the content on the detail page."
}
到,
{
"description": "Report Name",
"id": 1,
"load_date_time": "2019-02-12T05:14:28+00:00",
"children": [
1,
2,
3
],
"publish_date_time": "2018-09-03T00:00:00+00:00",
"summary": "Summary will also be present. Usually two to three brief sentences about the content on the detail page."
}
棉花糖模式:
class ChildIdSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ('id', )
ordered = True
class ParentSchema(ma.Schema):
children = fields.Nested('ChildIdSchema', many=True)
class Meta:
# Fields to expose
fields = ('id', 'description', 'children', 'summary', 'load_date_time', 'publish_date_time')
ordered = True
【问题讨论】:
标签: python flask sqlalchemy marshmallow