2

I need to construct a logical query with a repeated property and can't get it to work. I have a list object with topics.

topics = [u'string1', u'string2', ...]

I have a query object:

videos = Video.query()
videos.count()
=> 19

topics is a repeated string property

class Video
  topics = ndb.StringProperty(repeated=True)

I want to return videos that have a topic string1 OR string2. I also don't know the length of the list object before or I could just construct the query the long way with logical operators.

I tried doing this like the documentation suggests

videos.filter( Video.topics.IN([topics]) )

but that throws the error that IN expected a string not a list object.

How do I do this?

mehulkar
  • 4,895
  • 5
  • 35
  • 55

2 Answers2

8

Looks like topics is already a list. So you need to pass it without another list around it:

videos.filter( Video.topics.IN(topics) )
Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
1

For an arry of topics, you can use:

Video.query(Video.topics.IN(topics))

Or for a single string:

Video.query(Video.topics == topic)

source: https://cloud.google.com/appengine/docs/standard/python/ndb/queries

rcmstark
  • 1,031
  • 1
  • 14
  • 18