我可以比较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/