2013-01-07 52 views
0

我想查詢一個實體,但排除一堆鍵/ ID,我不想在結果中。這樣做的最好方法是什麼?ndb查詢排除多個鍵或ID

我想也許.IN運營商會幫助我,但無法弄清楚如何。

於是我想出了以下解決方案鏈單鍵排除OP:

q = models.Comment.query() 
for exclude_key in list_of_comment_keys_to_exclude: 
    q = q.filter(models.Comment.key != exclude_key) 
q = q.order(models.Comment.key) # without this: BadRequestError: The first sort property must be the same as the property to which the inequality filter is applied. 
q = q.order(models.Comment.creationTime) 

這似乎是工作,但它是去它的好方法嗎?

回答

5

這可能會起作用,但效率不高。獲得所有結果後,在用戶代碼中排除個別密鑰將會更便宜。例如:

q = models.Comment.query().order(...) 
results = [res for res in q.fetch() if res.key not in list_of_comment_keys_to_exclude] 
+0

啊好吧謝謝Guido! – joplaete