-
Given the following Models: class User(Model):
name = Field()
has_many('memberships', {'organizations': {'via': 'memberships'}})
class Organization(Model):
name = Field()
has_many('memberships', {'users': {'via': 'memberships'}})
class Membership(Model):
belongs_to('user', 'organization')
role = Field() How would one get a list of User records with membership at Organization X and role == 'admin'? The documentations talks about how to add a record with a role on the join table, but I can't figure out how to access the join table data from the User records. |
Beta Was this translation helpful? Give feedback.
Answered by
gi0baro
Jun 2, 2021
Replies: 1 comment 3 replies
-
@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 admin_users = org.users.where(Membership.role == "admin").select() with a single query using 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() |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Triquetra
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Triquetra you can reach the same result in different ways.
In case you have the organization record:
or without
switch
:with a single query using
switch
:with a single explicit query: