如何正确使用NDB查询和/或

时间:2013-03-26 13:57:05

标签: google-app-engine app-engine-ndb

我不确定我是否正确构建了ANDOR的查询。当我查询匹配标签时,我会收到一些匹配的视频。但是当我尝试第二个查询时,(为了确保视频与自身无关),查询将返回所有视频。

更新:我希望返回与video中至少有一个相同标签的视频,但返回的列表不包含video。基本上是一个related_videos功能。

from solveforx.lib.moonshots import Video
from google.appengine.ext import ndb

video = Video.query().get()
tags = video.tags or []

for tag in tags:
  print Video.query(Video.tags == tag).count() # returns 1
  print "-------"
  print Video.query(Video.tags == tag and Video.key != video.key) # returns total videos - 1
  print "========"
  # also tried this
  # print Video.query(Video.tags == tag, ndb.AND(Video.key != moonshot.key)).count() # returns 0
  # print Video.query(ndb.AND(ndb.AND(Video.tags == tag), ndb.AND(Video.key != video.key) )).count()

关于此问题查看the documentation,但不确定运营商的运作方式。

2 个答案:

答案 0 :(得分:9)

AND至少需要两个参数。你应该这样做:

Video.query(ndb.AND(Video.tags == tag, Video.key != video.key))

从您发布的链接中,您可以看到有关如何将其与ndb.OR结合使用的更多示例。

答案 1 :(得分:5)

这有效:

for tag in tags:
  Video.query(Video.tags == tag, Video.key != video.key)