我使用棉花糖将 SQLAlchemy 实体转储到JSON,如下所示:
class EntitySchema(ma.ModelSchema):
class Meta:
model = Entity
children = fields.List(Nested(ChildSchema(only=("id",))))
问题是上面的代码使用嵌套对象而不是纯int-list生成JSON:
{
...
"children": [{"id": 1}, {"id": 2}]
}
如何告诉棉花糖仅解析id
属性"children": [1, 2]
的值?
答案 0 :(得分:2)
使用Pluck
字段:
class EntitySchema(ma.ModelSchema):
class Meta:
model = Entity
children = fields.List(fields.Pluck(ChildSchema, "id"))