google-app-engine - 我可以比较 GAE 的 ndb 中单一类型的两个属性吗?

标签 google-app-engine google-cloud-datastore app-engine-ndb

我可以比较GAE的ndb中单一类型的两个属性吗?

class GameLog(ndb.Model):
    duration = ndb.IntegerProperty(default=0)
    time = ndb.IntegerProperty(default=0)

我需要比较这两个属性。我怎样才能做到这一点?

GameLog.duration > GameLog.time

最佳答案

要完成这种事情,您需要保存(预先计算的)结果,以便对其进行索引,并且您可以查询它。

为了使这更容易,ndb 为您提供 Computed Properties :

class GameLog(ndb.Model):
    duration = ndb.IntegerProperty(default=0)
    time = ndb.IntegerProperty(default=0)
    timeout = ndb.ComputedProperty(lambda self: self.duration > self.time)

您不需要自己维护此属性,每次 put() 实体时,都会计算并保存该值。现在您可以进行查询:

GameLog.query(GameLog.timeout == True)

关于google-app-engine - 我可以比较 GAE 的 ndb 中单一类型的两个属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29890298/

相关文章:

java - objectify 中的复杂查询

python - GAE 云数据存储 : Get most frequently written models

google-app-engine - Google AppEngine - 大数据存储读取

python - 如何从应用程序引擎中的数据存储区获取基于时间的实体

python - 如何根据条件动态构建查询?

java - Java 8 迁移后性能急剧下降(Google App Engine)

python - ndb 模型、装饰器、嵌套函数

google-app-engine - 指定 Google Cloud Endpoints 响应协议(protocol)

python - 从数据存储中的实体检索 ID

python - 如何使用 Expando 查询事先未知的多个属性?