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

时间:2015-04-27 08:03:52

标签: 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

1 个答案:

答案 0 :(得分:3)

要完成此类操作,您需要保存(预先计算的)结果,因此它已被编入索引,您可以查询它。

为了使这更容易,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)