ndb.ComputedProperty导致' RuntimeError:超出最大递归深度'

时间:2016-01-14 20:17:18

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

这是我的班级Stat。我正在为类添加一个新的计算属性,它将自动总结现有类中的各种计数

class Stat(ndb.Model):
   visit_count = ndb.IntegerProperty(default=0)
   exit_count  = ndb.IntegerProperty(default=0)


   # New code
   def _get_total_count(self):
      return self.visit_count + self.exit_count

   response_count = ndb.ComputedProperty(lambda self: self._get_total_count)

当我创建此类的实例并保存它时

stat = Stat(visit_count=0, exit_count=2)
stat.put()

我得到了这个例外:

  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
    return repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
    rep = prop._value_to_repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
    return repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
    rep = prop._value_to_repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
    return repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
    rep = prop._value_to_repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
    return repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
    rep = prop._value_to_repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
    return repr(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/key.py", line 357, in __repr__
    if self.app() != _DefaultAppId():
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/key.py", line 824, in _DefaultAppId
    return os.getenv('APPLICATION_ID', '_')
  File "/Users/antkong/dev/zeetings/zeetings-ve/bin/../lib/python2.7/os.py", line 515, in getenv
    return environ.get(key, default)
  File "/Users/antkong/dev/zeetings/zeetings-ve/bin/../lib/python2.7/UserDict.py", line 58, in get
    def get(self, key, failobj=None):
RuntimeError: maximum recursion depth exceeded

如果我删除了计算属性,put可以毫无问题地执行。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

response_count = ndb.ComputedProperty(lambda self: self._get_total_count)

表示您为ComputedProperty返回的值是函数 self._get_total_count - 您没有调用它。

response_count = ndb.ComputedProperty(lambda self: self._get_total_count())