【问题标题】:How to correctly use NDB Query AND / OR如何正确使用 NDB Query AND / OR
【发布时间】:2013-03-26 13:57:05
【问题描述】:

我不确定我是否正确地使用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,但不确定操作员的工作方式。

【问题讨论】:

  • Mehul,我有点不清楚,您对第二个条件查询的期望结果是什么?您的 cmets 显示两个查询都返回 1。哪一个是错误的?
  • 对不起,我用我真正打算做的事情更新了这个问题。还要回答我自己的问题!

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    这行得通:

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

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 2019-02-16
      • 2020-06-27
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多