0
0
Djangoframework~10 mins

pre_delete and post_delete signals in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - pre_delete and post_delete signals
Delete request triggered
pre_delete signal sent
Object deletion from DB
post_delete signal sent
Cleanup or follow-up actions
When deleting an object, Django first sends a pre_delete signal, then deletes the object, and finally sends a post_delete signal for any cleanup.
Execution Sample
Django
from django.db.models.signals import pre_delete, post_delete
from django.dispatch import receiver

@receiver(pre_delete, sender=MyModel)
def before_delete(sender, instance, **kwargs):
    print(f"About to delete: {instance}")

@receiver(post_delete, sender=MyModel)
def after_delete(sender, instance, **kwargs):
    print(f"Deleted: {instance}")
This code prints messages before and after deleting a MyModel instance.
Execution Table
StepSignal/EventActionInstance StateOutput
1Delete requestUser calls delete()Instance exists in DBNo output
2pre_deleteSignal sent before deletionInstance still existsPrints: About to delete: <instance>
3Delete objectObject removed from DBInstance removedNo output
4post_deleteSignal sent after deletionInstance no longer in DBPrints: Deleted: <instance>
5EndNo more actionsInstance deletedProcess complete
💡 Deletion process ends after post_delete signal is sent
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
instanceExists in DBExists in DBDeleted from DBDeleted from DBDeleted from DB
Key Moments - 3 Insights
Why does pre_delete signal see the instance still in the database?
Because pre_delete is sent before the actual deletion (see execution_table step 2), the instance still exists in the database at that moment.
Can post_delete signal access the instance data?
Yes, post_delete receives the instance data but the object is already deleted from the database (execution_table step 4). The instance is a copy passed to the signal.
What happens if deletion is canceled during pre_delete?
If an error or exception occurs in pre_delete handlers, the deletion can be stopped before step 3, so post_delete won't run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the instance at step 3?
AInstance still exists in the database
BInstance has been deleted from the database
CInstance is being deleted but still accessible
DInstance is recreated
💡 Hint
Check the 'Instance State' column at step 3 in the execution_table
At which step does the pre_delete signal run?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Signal/Event' column in the execution_table
If the pre_delete signal raises an error, what happens next?
Apost_delete runs but deletion is rolled back
BDeletion continues and post_delete runs
CDeletion stops and post_delete does not run
DNothing happens, deletion proceeds normally
💡 Hint
Refer to key_moments about cancellation during pre_delete
Concept Snapshot
Django deletion flow:
- pre_delete signal fires before object is deleted
- Object is deleted from database
- post_delete signal fires after deletion
Use pre_delete to prepare or cancel deletion
Use post_delete for cleanup after deletion
Full Transcript
When you delete an object in Django, the system first sends a pre_delete signal. This happens before the object is removed from the database, so the instance still exists and can be accessed. After that, Django deletes the object from the database. Then, it sends a post_delete signal, which happens after the object is gone. The post_delete signal still receives the instance data but the object no longer exists in the database. If an error happens during pre_delete, the deletion can stop and post_delete will not run. This lets you prepare or cancel deletions and clean up after deletions safely.