Evaluate a condition after put() in NDB and GAE

2019-07-19 01:29发布

I need to execute some code after i've put my entity in the datastore, with NDB.

let's say i have something like this.

ent=Entity()
ent.put()

def after_put():
    assert ...

How can i achieve that without calling manually the function? Is there some sort of trigger or callback i could use?

1条回答
小情绪 Triste *
2楼-- · 2019-07-19 01:34

Yes, you can use "hooks".

NDB offers a lightweight hooking mechanism. By defining a hook, an application can run some code before or after some type of operations; for example, a Model might run some function before every get(). A hook function runs when using the synchronous, asynchronous and multi versions of the appropriate method. For example, a "pre-get" hook would apply to all of get(), get_async(), and get_multi().

https://developers.google.com/appengine/docs/python/ndb/entities#hooks

查看更多
登录 后发表回答