1

我有以下架构:

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"
  }
}

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

4

1 回答 1

0

其实我想通了。这样做的方法是:

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

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

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

于 2021-06-16T22:53:45.260 回答