本文介绍了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(options = set([MEMBER,TRAINER,OWNER]),default =MEMBER,
required = True)

然后在表俱乐部我这里



<$ p俱乐部(GCModel):
@property
def成员(自我):
返回ClubMembership.query(ndb.AND(ClubMembership.club == self.key,
ClubMembership.membership_type ==会员,
ClubMembership.is_active == True))

我真正需要的是检索 User 的列表。我应该怎么做?



获取这个查询,比使用for循环获取每个成员使用它的关键字?

我试着用 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实例的列表。您需要为每个人获取成员键:

  memberships = ClubMembership.query(...)
member_keys = [会员中m为m.member]
members = ndb.get_multi(member_keys)


I've this table

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)

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))

what i actually need is to retrive the list of User. how should i do?

fetch this query, than have a for loop that fetches each member using its key?

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 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多对多,检索关系之一的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:44