Piyush Kumar
Piyush Kumar

Reputation: 191

Django post_delete instance is still present in the database

One of the keyword argument in he post_delete is the instance, which is the actual instance being deleted.

According to the django docs - Note that the object will no longer be in the database, so be very careful what you do with this instance.

I am testing the following code ->

@receiver(post_delete, sender=Dummy)
def post_delete_callback(**kwargs):
    print('inside post_delete ----------')
    instance = kwargs.get('instance')
    raise Exception('Exception from the post delete receiver')

But even after the exception, the object should be deleted in the database, but I am getting the exception, as well as the object is not getting deleted from the database.

In case of pre_delete signal this is justifiable, but what is with the post_save signal? Am I missing something here?

Upvotes: 1

Views: 1056

Answers (1)

JPG
JPG

Reputation: 88519

The deletion process executes in a atomic block - which means, either this will be executed successfully or will revert back to the previous state(just like nothing has happened!).

In your case, the Exception statement creates a blocker to the execution flow and the DB/Django reverts the action (ie, the delete action) to the original state. This makes the objects still not deleted in the DB.

If you remove the Exception statement from your signal, you will not see the object again in the Database.

Upvotes: 3

Related Questions