I want to filter the nested field with is_active
column as True
in Marshmallow 3
Consider following scenario
I have 3 tables
users (id, name)
organizations (id, name)
organization_user(id, organization_id, user_id, is_active)
Now I'm trying to print all the organization with its members who are active. (There are some active and inactive members)
I have following schema
class OrganizationSchema(ma.ModelSchema):members_list = fields.Nested(OrgnizationUserSchema, many=True, exclude=('checklist', ))class OrgnizationUserSchema(ma.ModelSchema):user_list = fields.Nested(UserSchema)
Now in my action following is the code
organization_schema = OrganizationSchema(many=True)
#Query for list of organization
organization_list = Organization.query.all()
organization_schema.dump(organization_list)
Following is Output
[{'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}]}
]
I want output with member who has 'is_active':True
as follows
[{'id': 1,'name': 'abc','members_list': [{'id':1, 'organization_id': 1, 'user_id':1, 'is_active':True}]}
]
Marshmallow provides a decorator @post_dump
. Problem here is Query brings all data and then we filter it with decorator @post_dump
.
But the flow should be like, while querying there should be some way to filter the data and not post query filtering.