[英]datastore ndb, query using ndb.ComputedProperty within a projection is safe?
我将缩略图72x72编码的base64存储在ndb.TextProperty()中
模型结构类似于:
class Article(ndb.Model):
title = ndb.StringProperty()
body = ndb.TextProperty()
tags = ndb.StringProperty(repeated=True, indexed=True)
thumbnail = ndb.TextProperty()
has_thumbnail = ndb.ComputedProperty(
lambda self: True if self.thumbnail else False)
enable = ndb.BooleanProperty(default=True)
使用投影查询的方法是:
@classmethod
def get_articles(cls):
q = Aricle.query(
True == Article.enable,
projection = [
Article.title
Article.has_thumbnail
])
return q.get()
由于未对ndb.TextProperty()
进行索引,因此无法在投影上获取它们,因此我尝试使用ndb.ComputedProperty
并注意到该方法正在工作。
我的主要问题是要知道这是否是正确的查询方式,基本上我只是想通过查询返回文章标题和缩略图,或者知道文章是否具有缩略图。
没错,计算属性将与投影查询一起使用。
唯一需要注意的警告是,查询将使用持久保存到数据存储中的计算出的属性值。 因此,如果您仅添加了计算所得的属性,则对于先前添加的实体,该属性将不会出现在数据存储区中。 您将需要重新放置这些实体以保留新属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.