简体   繁体   English

数据存储区ndb,在投影内使用ndb.ComputedProperty查询是否安全?

[英]datastore ndb, query using ndb.ComputedProperty within a projection is safe?

I am storing thumbnails 72x72 encoded base64 in a ndb.TextProperty() 我将缩略图72x72编码的base64存储在ndb.TextProperty()中

The model structure is something like: 模型结构类似于:

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)   

The method for query using a projection is: 使用投影查询的方法是:

@classmethod
def get_articles(cls):
    q = Aricle.query(
        True == Article.enable,
        projection = [
            Article.title
            Article.has_thumbnail
        ])
     return q.get()

Since the ndb.TextProperty() are not indexed I can not get them on the projections, therefore I tried with a ndb.ComputedProperty and notice that was working. 由于未对ndb.TextProperty()进行索引,因此无法在投影上获取它们,因此我尝试使用ndb.ComputedProperty并注意到该方法正在工作。

My main question is to know if this is a correct way of doing the query, basically I just want a query to return the title of the article and the thumbnail or to know if the article has thumbnail. 我的主要问题是要知道这是否是正确的查询方式,基本上我只是想通过查询返回文章标题和缩略图,或者知道文章是否具有缩略图。

That is correct, the computed property will work with a projection query. 没错,计算属性将与投影查询一起使用。

The only caveat to be aware of is that the query will use the computed property value that is persisted to data store. 唯一需要注意的警告是,查询将使用持久保存到数据存储中的计算出的属性值。 So if you just added the computed property, it will not be present in the data store for previously-added entities; 因此,如果您仅添加了计算所得的属性,则对于先前添加的实体,该属性将不会出现在数据存储区中。 you would need to re-put those entities to persist the new property. 您将需要重新放置这些实体以保留新属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM