使用 flask restful 整理列表中的项目
Marshaling items in lists using flask restful
我正在编写 Flask-RESTFUL 资源。它 returns 一个名为 DocumentSet
的 Model
对象,具有以下结构:
DocumentSet:
id: int
documents: list of Document
Document
是另一个具有以下结构的 Model
对象:
Document:
id: int
...other fields...
我想写一个 @marshal_with
装饰器 returns DocumentSet
id 以及 Document
id 的列表,如下所示:
{
id: 5,
document_ids: [1, 2, 3]
}
我一直在用头撞 output marshaling syntax 无济于事。我尝试过的一些事情:
{'id': fields.Integer, 'document_ids':fields.List(fields.Integer, attribute='documents.id')}
{'id': fields.Integer, 'document_ids':fields.List(fields.Nested({'id':fields.Integer}), attribute='documents')}
魔法咒语是什么?
魔法咒语是
{'id': fields.Integer, 'name': fields.String, 'document-ids':{'id': fields.Integer}}
它就在文档的“Complex Structures”段落中。
我正在编写 Flask-RESTFUL 资源。它 returns 一个名为 DocumentSet
的 Model
对象,具有以下结构:
DocumentSet:
id: int
documents: list of Document
Document
是另一个具有以下结构的 Model
对象:
Document:
id: int
...other fields...
我想写一个 @marshal_with
装饰器 returns DocumentSet
id 以及 Document
id 的列表,如下所示:
{
id: 5,
document_ids: [1, 2, 3]
}
我一直在用头撞 output marshaling syntax 无济于事。我尝试过的一些事情:
{'id': fields.Integer, 'document_ids':fields.List(fields.Integer, attribute='documents.id')}
{'id': fields.Integer, 'document_ids':fields.List(fields.Nested({'id':fields.Integer}), attribute='documents')}
魔法咒语是什么?
魔法咒语是
{'id': fields.Integer, 'name': fields.String, 'document-ids':{'id': fields.Integer}}
它就在文档的“Complex Structures”段落中。