烧瓶棉花糖从嵌套字典中删除键

时间:2021-06-16 18:19:42

标签: marshmallow flask-marshmallow

我有以下架构:

class PublisherSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        fields = ('name',)
        model = Publisher


class JournalSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        fields = ('title', 'publisher')
        model = Journal
        ordered = True

    publisher = ma.Nested(PublisherSchema)

当我转储 JournalSchema 时,我希望结果是:

{
  "title": "hello",
  "publisher: "bye"
}

但现在它转储为:

{
  "title": "hello",
  "publisher": {
    "name": "bye"
  }
}

如何嵌套发布者值但不显示键?

1 个答案:

答案 0 :(得分:0)

其实我想通了。方法是:

class JournalSchema(ma.SQLAlchemyAutoSchema):
    publisher = fields.String(attribute="publisher.name")

    class Meta:
        fields = ('title', 'publisher')
        model = Journal
        ordered = True

这认为“发布者”是引用模型的关系。