whitenoisedb
whitenoisedb

Reputation: 403

Correct way to insert another model entity inside _post_put_hook?

I wonder if I could make a transactional insert operation inside the _post_put_hook(), or how should I deal with put() errors before any operation inside _post_put_hook().

I'm using Google App Engine in Python. My code is something like this,

class Event(ndb.Model):
  ...
  def _post_put_hook(self, future):
    Device.get_or_insert(self.device_id, device_name=self.device_name, ...)
  ...

Despite get_or_insert() is transactional I'm not sure if the new Device entity will have the correct data from Event's put()

Upvotes: 0

Views: 59

Answers (1)

sander
sander

Reputation: 91

This is exactly what the Future argument passed to the hook is for. You can call it's check_success() method to ensure the put() operation succeeded.

class Event(ndb.Model):

  def _post_put_hook(self, future):
    if not future or future.check_success() is None:
      Device.get_or_insert(self.device_id, device_name=self.device_name, ...)

You can read more about the Future class in the official docs.

Upvotes: 1

Related Questions