Skip to content

How to filter records by join table #341

Answered by gi0baro
Triquetra asked this question in Q&A
Discussion options

You must be logged in to vote

@Triquetra you can reach the same result in different ways.

In case you have the organization record:

org = Organization.get(name="X")
admin_users = org.users.switch(Membership).where(lambda m: m.role == "admin").select()

or without switch:

admin_users = org.users.where(Membership.role == "admin").select()

with a single query using switch:

admin_users = User.all().join('organizations').switch(Organization).where(
    lambda o: o.name == "X"
).switch(Membership).where(
    lambda m: m.role == "admin"
).select()

with a single explicit query:

User.all().join('organizations').where(
    (Organization.name == "X") & (Membership.role == "admin")
).select()

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Triquetra
Comment options

@gi0baro
Comment options

@Triquetra
Comment options

Answer selected by Triquetra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #340 on June 02, 2021 13:10.