My NDB model class has repeated property:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
Is there any way to query for all entities havings tags
equal to ['music', 'cinema']
? Ie each entity returned should have music
and cinema
tag at the same time and shouldn't have other tags
. The GAE doc says that
You cannot compare repeated properties to list objects (the Datastore won't understand it)
Will I have to fetch all entities with one tag and then filter it manually?
Storing a serialized/hashed version of the list and querying for an exact match against that will likely be more efficient than fetching all of your entities:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
tagset = ndb.ComputedProperty(lambda self: ','.join(self.tags.sort()))
Then to query use the same serialization on your search-tags:
q = Something.query(cls.tagset == ','.join(sorted(['music', 'cinema'])))
Yes, you can use the IN
property, which uses a list object when Querying for Repeated Properties :
Something.tags.IN(['music', 'cinema'])
To see if both tags are present, you can use the AND
operation:
Something.tags.query(ndb.AND(Something.tags == 'music',
Something.tags == 'cinema'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.