【问题标题】:datastore ndb, query using ndb.ComputedProperty within a projection is safe?数据存储 ndb,在投影中使用 ndb.ComputedProperty 进行查询是否安全?
【发布时间】:2014-07-15 12:12:51
【问题描述】:

我将 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 并注意到它正在工作。

我的主要问题是知道这是否是一种正确的查询方式,基本上我只想查询返回文章的标题和缩略图,或者知道文章是否有缩略图。

【问题讨论】:

    标签: python python-2.7 google-app-engine google-cloud-datastore app-engine-ndb


    【解决方案1】:

    没错,计算属性将与投影查询一起使用。

    唯一需要注意的是,查询将使用持久化到数据存储的计算属性值。因此,如果您只是添加了计算属性,则它不会出现在先前添加的实体的数据存储中;您需要重新放置这些实体以保留新属性。

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多