ndb 多对多,检索关系之一的列表
ndb many to many, retrieve list of one of the relation
我有这张桌子
class ClubMembership(GCModel):
member = ndb.KeyProperty(kind='User', required=True)
club = ndb.KeyProperty(kind='Club', required=True)
is_active = ndb.BooleanProperty(default=True)
membership_type = ndb.StringProperty(choices=set(["MEMBER", "TRAINER", "OWNER"]), default="MEMBER",
required=True)
然后在 Table Club
我有这个
class Club(GCModel):
@property
def members(self):
return ClubMembership.query(ndb.AND(ClubMembership.club == self.key,
ClubMembership.membership_type == "MEMBER",
ClubMembership.is_active == True))
我真正需要的是检索 User
的列表。我该怎么办?
获取此查询,然后使用 for 循环获取每个 member
使用其键?
我尝试使用 member
上的投影来使用 ndb.get_multi
,但它不起作用。
像这样
members= ClubMembership.query(ndb.AND(ClubMembership.club == self.key,
ClubMembership.membership_type == "MEMBER",
ClubMembership.is_active == True),projection=['member']).fetch()
ndb.get_multi(members)
get_multi
适用于键列表,您有一个 ClubMembership 实例列表。您需要为每个人获取 member
密钥:
memberships = ClubMembership.query(...)
member_keys = [m.member for m in memberships]
members = ndb.get_multi(member_keys)
您还应考虑将会员信息存储在用户实体内。这样你就可以在一个查询中获取所有用户,速度要快得多。
class Membership(ndb.Model):
club = ndb.KeyProperty(kind='Club', required=True)
is_active = ndb.BooleanProperty(default=True)
membership_type = ndb.StringProperty(
choices=set(["MEMBER", "TRAINER", "OWNER"]), default="MEMBER", required=True)
class User(ndb.Model):
memberships = ndb.StructuredProperty(Membership, repeated=True)
class Club(ndb.Model):
@property
def members(self):
membership = Membership(
User.memberships.club == self.key,
User.memberships.membership_type == 'MEMBER',
User.memberships.is_active == True)
return User.query(User.memberships == membership)
我有这张桌子
class ClubMembership(GCModel):
member = ndb.KeyProperty(kind='User', required=True)
club = ndb.KeyProperty(kind='Club', required=True)
is_active = ndb.BooleanProperty(default=True)
membership_type = ndb.StringProperty(choices=set(["MEMBER", "TRAINER", "OWNER"]), default="MEMBER",
required=True)
然后在 Table Club
我有这个
class Club(GCModel):
@property
def members(self):
return ClubMembership.query(ndb.AND(ClubMembership.club == self.key,
ClubMembership.membership_type == "MEMBER",
ClubMembership.is_active == True))
我真正需要的是检索 User
的列表。我该怎么办?
获取此查询,然后使用 for 循环获取每个 member
使用其键?
我尝试使用 member
上的投影来使用 ndb.get_multi
,但它不起作用。
像这样
members= ClubMembership.query(ndb.AND(ClubMembership.club == self.key,
ClubMembership.membership_type == "MEMBER",
ClubMembership.is_active == True),projection=['member']).fetch()
ndb.get_multi(members)
get_multi
适用于键列表,您有一个 ClubMembership 实例列表。您需要为每个人获取 member
密钥:
memberships = ClubMembership.query(...)
member_keys = [m.member for m in memberships]
members = ndb.get_multi(member_keys)
您还应考虑将会员信息存储在用户实体内。这样你就可以在一个查询中获取所有用户,速度要快得多。
class Membership(ndb.Model):
club = ndb.KeyProperty(kind='Club', required=True)
is_active = ndb.BooleanProperty(default=True)
membership_type = ndb.StringProperty(
choices=set(["MEMBER", "TRAINER", "OWNER"]), default="MEMBER", required=True)
class User(ndb.Model):
memberships = ndb.StructuredProperty(Membership, repeated=True)
class Club(ndb.Model):
@property
def members(self):
membership = Membership(
User.memberships.club == self.key,
User.memberships.membership_type == 'MEMBER',
User.memberships.is_active == True)
return User.query(User.memberships == membership)