我使用棉花糖将我的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]
?
使用Pluck
字段:
class EntitySchema(ma.ModelSchema):
class Meta:
model = Entity
children = fields.List(fields.Pluck(ChildSchema, "id"))