当前位置: 动力学知识库 > 问答 > 编程问答 >

google app engine - How to correctly use NDB Query AND / OR

问题描述:

I'm not sure if I'm constructing the query with AND and OR properly. When I query to just match tag, I get a handful of matching videos. But when I try the second query, (to make sure that a video isn't related to itself), the query returns all videos.

Update: I'm looking to return videos that have at least one of the same tags as video in question, but the list returned does not include video. Basically a related_videos feature.

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()

Looking at the documentation on this, but not sure how the operators are working.

网友答案:

AND takes at least two parameters. You should do:

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

From the link that you posted you can see more examples on how to combine it with ndb.OR as well.

网友答案:

This worked:

for tag in tags:
  Video.query(Video.tags == tag, Video.key != video.key)
分享给朋友:
您可能感兴趣的文章:
随机阅读: