在 Flask Marshmallow 中过滤嵌套字段

问题描述 投票:0回答:2

我想将

is_active
列的嵌套字段过滤为 Marshmallow 3 中的
True
考虑以下场景 我有3张桌子

users (id, name)
organizations (id, name)
organization_user(id, organization_id, user_id, is_active)

现在我正在尝试打印所有组织及其活跃成员。 (有活跃会员和不活跃会员)

我有以下架构

class OrganizationSchema(ma.ModelSchema):
    members_list = fields.Nested(OrgnizationUserSchema, many=True, exclude=('checklist', ))

class OrgnizationUserSchema(ma.ModelSchema):
    user_list = fields.Nested(UserSchema)

现在在我的操作中,以下是代码

organization_schema = OrganizationSchema(many=True)
#Query for list of organization
organization_list = Organization.query.all()
organization_schema.dump(organization_list)

以下是输出

[
    {
    'id': 1,
    'name': 'abc',
    'members_list': [
        {'id':1, 'organization_id': 1, 'user_id':1, 'is_active':True},
        {'id':1, 'organization_id': 1, 'user_id':2, 'is_active':False}
        ]
    }
]

我想要与具有

'is_active':True
的成员一起输出,如下

[
    {
    'id': 1,
    'name': 'abc',
    'members_list': [
        {'id':1, 'organization_id': 1, 'user_id':1, 'is_active':True}
        ]
    }
]

Marshmallow 提供了一个装饰器

@post_dump
。这里的问题是 Query 带来了所有数据,然后我们用装饰器
@post_dump
对其进行过滤。 但流程应该是这样的,在查询时应该有某种方法来过滤数据,而不是查询后过滤。

python sqlalchemy marshmallow
2个回答
3
投票

我走了另一条路。我有布料、设计和剩余物。对于每种面料,我需要获取所有设计,对于每种设计,我将获取指定城市的剩余部分。

class ClothSchema(Schema):
    id = fields.Integer(dump_only=True)
    name = fields.String(validate=not_blank)
    type = fields.Nested(ClothTypeSchema)
    designs = fields.Nested(DesignSchema, many=True)

class DesignSchema(Schema):
    id = fields.Integer(dump_only=True)
    name = fields.String(validate=not_blank)
    remainders = fields.Nested(RemainderSchema, many=True)

class RemainderSchema(Schema):
    id = fields.Integer(dump_only=True)
    value = fields.String()
    city = fields.String()

我在控制器中获取所需的数据,这样就不会在旅途中请求它们。

    db.session.query(Cloth)
    .join(Cloth.designs)
    .join(Design.remainders)
    .filter(Remainder.city == city)
    .options(contains_eager("designs").contains_eager("remainders"))

结果,我得到了剩余的所有布料和所有设计。如果没有指示设计余数,则不会显示。

{
  "attributes": {
    "designs": {
      "data": [
        {
          "attributes": {
            "name": "Amely 10 ", 
            "remainders": {
              "data": [
                {
                  "attributes": {
                    "city": "CityEnum.MOSCOW", 
                    "value": "333"
                  }, 
                  "id": 9318, 
                  "type": "remainder"
                }
              ]
            }
          }, 
          "id": 365, 
          "type": "design"
        } 
      ], 
      "links": {
        "self": "/designs"
      }
    }, 
    "name": "Amely", 
    "rapport": null, 
    "type": {} 
  }, 
  "id": 22, 
  "type": "cloth"
}

0
投票

我知道你没有提到 SQLAlchemy,但我遇到了同样的问题,我最终得到了一个使用

primaryjoin
的解决方案(可能对某人有帮助)

SQLAlchemy 文档

我的问题与软删除有关,因此我没有修改 Marshmallow,而是为 SqlalAlchemy 模型定义了一个primaryjoin,如下所示。这允许我将产品作为公司的子列表返回,但仅限于未删除的产品。

[代码仅说明]

class Company():
    __tablename__ = "companies"

    products = db.relationship(
        "Product",
        backref="company",
        primaryjoin="and_(Company.id==Product.company_id, "
                    "Product.deleted == True)"
    )
© www.soinside.com 2019 - 2024. All rights reserved.