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

meh*_*kar 2 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()
Run Code Online (Sandbox Code Playgroud)

查看相关文档,但不确定运算符是如何工作的.

Lip*_*pis 9

AND至少需要两个参数.你应该做:

Video.query(ndb.AND(Video.tags == tag, Video.key != video.key))
Run Code Online (Sandbox Code Playgroud)

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


meh*_*kar 5

这工作:

for tag in tags:
  Video.query(Video.tags == tag, Video.key != video.key)
Run Code Online (Sandbox Code Playgroud)