2013-01-07 49 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