本文介绍了ndb 多对多,检索关系之一的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这张桌子
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)
然后在表中 Club
我有这个
Then in the Table Club
i've this
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
的列表.我该怎么办?
what i actually need is to retrive the list of User
. how should i do?
获取此查询,而不是使用 for 循环使用其键获取每个 member
?
fetch this query, than have a for loop that fetches each member
using its key?
我尝试使用 ndb.get_multi
使用 member
上的投影,但它不起作用.像这样
i tried to use ndb.get_multi
using the projection on member
but it does not work.something like this
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
键:
get_multi
works on a list of keys, you have a list of ClubMembership instances. You need to get the member
key for each one:
memberships = ClubMembership.query(...)
member_keys = [m.member for m in memberships]
members = ndb.get_multi(member_keys)
这篇关于ndb 多对多,检索关系之一的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!