0

我将 a_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)

想法?

4

1 回答 1

3

在这种情况下 self.settings 不是实际的键而是 Models 属性,因为这是一个类方法而不是实例方法。你需要处理未来的对象。
这里的文档: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()

于 2012-04-25T16:08:38.427 回答