小编rak*_*esh的帖子

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

我想用棉花糖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, …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy marshmallow

6
推荐指数
1
解决办法
129
查看次数

标签 统计

marshmallow ×1

python ×1

sqlalchemy ×1