如何过滤具有完全相同重复属性的 NDB 实体?
How to filter NDB entities having exactly the same repeated properties?
我的 NDB 模型 class 重复了 属性:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
有什么方法可以查询 tags
等于 ['music', 'cinema']
的所有实体吗? IE。返回的每个实体应同时具有 music
和 cinema
标签,并且不应具有其他 tags
。 The GAE doc 说
You cannot compare repeated properties to list objects (the Datastore
won't understand it)
我是否必须使用一个标签获取所有实体,然后手动过滤?
是的,您可以使用 IN
属性,它在 Querying for Repeated Properties:
时使用列表对象
Something.tags.IN(['music', 'cinema'])
要查看两个标签是否都存在,您可以使用 AND
操作:
Something.tags.query(ndb.AND(Something.tags == 'music',
Something.tags == 'cinema'))
存储列表的 serialized/hashed 版本并查询与之完全匹配的内容可能比获取所有实体更有效:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
tagset = ndb.ComputedProperty(lambda self: ','.join(self.tags.sort()))
然后在您的搜索标签上使用相同的序列化进行查询:
q = Something.query(cls.tagset == ','.join(sorted(['music', 'cinema'])))
我的 NDB 模型 class 重复了 属性:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
有什么方法可以查询 tags
等于 ['music', 'cinema']
的所有实体吗? IE。返回的每个实体应同时具有 music
和 cinema
标签,并且不应具有其他 tags
。 The GAE doc 说
You cannot compare repeated properties to list objects (the Datastore won't understand it)
我是否必须使用一个标签获取所有实体,然后手动过滤?
是的,您可以使用 IN
属性,它在 Querying for Repeated Properties:
Something.tags.IN(['music', 'cinema'])
要查看两个标签是否都存在,您可以使用 AND
操作:
Something.tags.query(ndb.AND(Something.tags == 'music',
Something.tags == 'cinema'))
存储列表的 serialized/hashed 版本并查询与之完全匹配的内容可能比获取所有实体更有效:
class Something(ndb.Model):
tags = ndb.StringProperty(repeated=True)
tagset = ndb.ComputedProperty(lambda self: ','.join(self.tags.sort()))
然后在您的搜索标签上使用相同的序列化进行查询:
q = Something.query(cls.tagset == ','.join(sorted(['music', 'cinema'])))