Is the model specific put() called for each entity when db.put() is called with a list of entities?

89 views Asked by At

I've got some models on my GAE app, and I've overriden put() on some of them. When I call db.put() with a list of these entites, is there a guarantee that the overriden put() on each entity will be called?

What I'm seeing right now is that the entities are just getting saved without it being called. Is there any good way to make sure stuff is done before every save, even batches?

2

There are 2 answers

0
David Underhill On BEST ANSWER

No. You need to monkeypatch db.put() too. For a good example of this, check out Nick Johnson's excellent blog post on Pre- and post- put hooks for Datastore models.

If you look at the source code for the db module, you'll see that db.put() does not call the entity's put() function.

0
user103576 On

You could try something like:

class SomeModel(db.Model):
    aprop = db.IntegerProperty()

    def _populate_internal_entity(self, *args, **kwargs):
        logging.warn('about to Put() SomeModel: %r', self)
        return super(SomeModel, self)._populate_internal_entity(*args, **kwargs)

However, there is probably a better way to do it. If you are trying to set some properties you should check out custom property classes. If you are trying to do logging or caching you should investigate datastore hooks.