过滤烧瓶棉花糖中的嵌套字段

rak*_*esh 6 python sqlalchemy marshmallow

我想用棉花糖3中的is_active列过滤嵌套字段,True请考虑以下情形,我有3个表

users (id, name)
organizations (id, name)
organization_user(id, organization_id, user_id, is_active)
Run Code Online (Sandbox Code Playgroud)

现在,我尝试将所有组织及其活动成员打印出来。(有一些活跃和不活跃的成员)

我有以下架构

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

class OrgnizationUserSchema(ma.ModelSchema):
    user_list = fields.Nested(UserSchema)
Run Code Online (Sandbox Code Playgroud)

现在在我的行动中,下面的代码

organization_schema = OrganizationSchema(many=True)
#Query for list of organization
organization_list = Organization.query.all()
organization_schema.dump(organization_list)
Run Code Online (Sandbox Code Playgroud)

以下是输出

[
    {
    '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}
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

我想要与具有'is_active':True以下内容的成员进行输出

[
    {
    'id': 1,
    'name': 'abc',
    'members_list': [
        {'id':1, 'organization_id': 1, 'user_id':1, 'is_active':True}
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

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

小智 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()
Run Code Online (Sandbox Code Playgroud)

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

    db.session.query(Cloth)
    .join(Cloth.designs)
    .join(Design.remainders)
    .filter(Remainder.city == city)
    .options(contains_eager("designs").contains_eager("remainders"))
Run Code Online (Sandbox Code Playgroud)

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

{
  "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"
}
Run Code Online (Sandbox Code Playgroud)