KeyProperty对象没有属性get

时间:2012-04-25 15:56:24

标签: google-app-engine app-engine-ndb

我将_post_put_hook放入我的一个NDB模型类型中,这样只要放置了该类型的实体,它就会使memcache密钥无效。此密钥由设置密钥的urlsafe版本组成。但是,当此代码运行时,它会说:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 68, in get
    result = func(*args)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 154, in pub_refreshSandbox
    team_key = s.create.team("Cool Group")
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\GlobalUtilities.py", line 534, in team
    new_team.put()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2902, in _put
    return self._put_async(**ctx_options).get_result()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 320, in get_result
    self.check_success()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 315, in check_success
    self.wait()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 299, in wait
    if not ev.run1():
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\eventloop.py", line 219, in run1
    delay = self.run0()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\eventloop.py", line 181, in run0
    callback(*args, **kwds)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 454, in _on_future_completion
    self._help_tasklet_along(gen, val)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 368, in _help_tasklet_along
    self.set_result(result)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 264, in set_result
    callback(*args, **kwds)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\DataModels.py", line 182, in _post_put_hook
    tools.expireCache('allteams-' + self.settings.get().websafe)
AttributeError: 'KeyProperty' object has no attribute 'get'

以下是相关的模型类:

class Team(ndb.Expando):
    name = ndb.StringProperty()
    show_team = ndb.BooleanProperty()
    settings = ndb.KeyProperty()

    @classmethod
    def _post_put_hook(self, future):
        memcache.delete('allteams-' + self.settings.get().websafe)

想法?

1 个答案:

答案 0 :(得分:3)

在这种情况下,self.settings不是实际的键,而是Models属性,因为这是一个classmethod而不是实例方法。你需要处理未来的对象 这里是文档:https://developers.google.com/appengine/docs/python/ndb/futureclass

在这种情况下:

@classmethod
def _post_put_hook(self, future):
    entitykey = future.get_result()
    entity    = entitykey.get()
    memcache.delete('allteams-' + entity.settings.get().websafe)

不确定websafe为您做了什么。也许你的意思是entity.settings.urlsafe()