-2

I am using NDB in an Appengine project. I am using _post_delete_hook for doing some operations after some entity is deleted. In this method I have the key. But when I do key.get() I get an error. Here is some example code.

[friends/models.py]

from ndb import models
from users.models import User

class FriendShip(models.Model):
    user = models.KeyProperty(kind=User)
    friend = models.KeyProperty(kind=User)

    @classmethod
    def _post_delete_hook(cls, key, future):
        signals.post_delete.send(cls, instance=key)

[some-other-filer.py]

# connected method to post_detele_hook
def ended_friendship(sender, **kwargs):
    key = kwargs.get('instance', None)
    if key:
        user = key.get().user # raise a non existing entity error

Some help?

This is the doc about hooks. http://code.google.com/appengine/docs/python/ndb/entities.html#hooks

proppy
  • 10,495
  • 5
  • 37
  • 66
francofuji
  • 17
  • 3

1 Answers1

2

For your use case (wanting to access the entity in your method), a _pre_delete_hook might make more sense. See http://code.google.com/appengine/docs/python/ndb/modelclass.html#Model__pre_delete_hook .

Amy U.
  • 2,227
  • 11
  • 11
  • Thanks for your response. But there is a grant of a successfully delete operation with a pre_hook? In my case I want a post_delete_hook for decrementing some total count that own an entity related to the deleted entity. – francofuji Mar 07 '12 at 19:18
  • You'll have to find a mechanism to pass the information from the pre-delete hook to the post-delete hook. Probably some kind of global dict indexed by key might work (be sure to delete the key in the post-delete hook). – Guido van Rossum Mar 07 '12 at 20:13
  • Could not be the deleted entity a param in the hook like Django model signals? Im thinking right now that if I would like to know if some entity is being saved for first time or just updated I should make some kind of game with both pre_put and post_put hooks. It would be great to have a created param in post_put_hook. Like Django too. – francofuji Mar 07 '12 at 21:06