[英]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.