Flask Marshmallow 序列化具有额外字段的多对多关系

sor*_*ike 5 python serialization sqlalchemy flask marshmallow

我在带有序列化模型对象的 Flask 应用程序中遇到一个问题,该对象与存储在关联表中的额外字段存在多对多关系。我想要一个像这样的序列化数据:

{
    "id": "123",
    "name": "name",
    "mobile": "phone number",
    "interest": [1, 2, 3]
    "_embedded": {
        "interest": [
            {
                "id": 1,
                "name": "ECONOMIC",
                "active": true,
            },
            {
                "id": 2,
                "name": "POETRY",
                "active": true,
            },
            {
                "id": 3,
                "name": "SPORT",
                "active": false,
            },
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我设法准备了一个必要的模型,如下所示:

class OwnerInterests(db.Model):
    owner_id = db.Column(db.Integer, db.ForeignKey('owners.id'), primary_key=True)
    interest_id = db.Column(db.Integer, db.ForeignKey('interests.id'), primary_key=True)
    active = db.Column(db.Boolean)
    interest = db.relationship('Interests', back_populates='owners')
    owner = db.relationship('Owners', back_populates='interests')


class Owners(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    mobile = db.Column(db.String)
    interests = db.relationship('OwnersInterests', back_populates='owner')


class Interests(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    owners = db.relationship('OwnersInterests', back_populates='interest')
Run Code Online (Sandbox Code Playgroud)

但现在我想知道如何使用棉花糖模式准备 sqlalchemy 查询。有什么想法吗?

编辑 :

我当前的棉花糖架构如下所示:

class InterestSchema(ma.ModelSchema):
    class Meta:
        model = Interests
        exclude = ('owners',)


class OwnerSchema(ma.ModelSchema):
    interests = ma.Nested(InterestSchema, many=True)

    class Meta:
        model = Owners
Run Code Online (Sandbox Code Playgroud)

Tom*_*ert 4

该架构为您提供了与您的规范非常相似的内容:

from marshmallow import Schema, fields


class InterestSchema(Schema):
    class Meta:
        fields = ('id', 'name')
        ordered = True


class OwnerInterestSchema(Schema):
    interest = fields.Nested(InterestSchema)

    class Meta:
        fields = ('id', 'interest', 'active')
        ordered = True


class OwnerSchema(Schema):
    interests = fields.Nested(OwnerInterestSchema, many=True)

    class Meta:
        fields = ('id', 'name', 'mobile', 'interests')
        ordered = True
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样序列化数据(请注意,我的模型的名称与您的模型的名称不完全相同):

>>> from app.serialisation import OwnerSchema
>>> from app.models import Owner
>>> data = OwnerSchema().dump(Owner.query.get(1))
>>> from marshmallow import pprint
>>> pprint(data)
{"id": 1, "name": "John", "mobile": "07123456789", "interests": [{"interest": {"id": 1, "name": "Economics"}, "active": true}, {"interest": {"id": 2, "name": "Poetry"}, "active": true}, {"interest": {"id": 3, "name": "Sport"}, "active": false}]}
Run Code Online (Sandbox Code Playgroud)

让我缩进该输出,以便您可以看到发生了什么:

{
  "id": 1,
  "name": "John",
  "mobile": "07123456789",
  "interests": [
    {
      "interest": {
        "id": 1,
        "name": "Economics"
      },
      "active": true
    },
    {
      "interest": {
        "id": 2,
        "name": "Poetry"
      },
      "active": true
    },
    {
      "interest": {
        "id": 3,
        "name": "Sport"
      },
      "active": false
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以对其进行调整以使用模型加排除范例。如果您确实希望"_embedded"在 JSON 中包含该字段,则可能需要一个自定义字段,如此处所述

您还可以使用自定义字段来展平您的兴趣,并将该字段置于与和"active"相同的级别,但我认为这会产生误导。"id""name"