2017-02-14 153 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

工作就像一個魅力,謝謝你! –