2017-02-14 94 views
1

下面这组查询连接了一系列配置文件,并返回到网站。订购查询

我想用这些做一个查询,因为我想在整个配置集合上使用order函数。

# Get all users for who 'currentUser' is an assistant for. 
users = Users.query(Users.assistant.IN(currentUser)).fetch() 

# Get all profiles that belong to those users and have not been deleted. 
for user in users: 
    profiles = profiles + Profiles.query(
      ndb.AND(
      Profiles.user == user.username, 
      Profiles.deleted == False 
     ) 
    ).order(Profiles.history).fetch() # Order all profiles by timestamp 

回答

0

您要查找的内容将是,在某种程度上,相当于:

# get the list of usernames obtained above 
usernames = [user.username for user in users] 

profiles = Profiles.query(ndb.AND(Profiles.user.IN(usernames), 
            Profiles.deleted == False)) 
         .order(Profiles.history).fetch() 

但是,这将有性能问题,如果usernames包含大量元素(显然是在10个左右),见Does the NDB membership query ("IN" operation) performance degrade with lots of possible values?

+0

工作就像一个魅力,谢谢你! –