0
0
Djangoframework~10 mins

pre_save and post_save signals in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - pre_save and post_save signals
Model instance about to save
pre_save signal triggered
Instance saved to DB
post_save signal triggered
Further actions or cleanup
When saving a model, Django first triggers pre_save signal before saving, then saves the instance, and finally triggers post_save signal after saving.
Execution Sample
Django
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver

@receiver(pre_save, sender=MyModel)
def before_save(sender, instance, **kwargs):
    print('Before saving:', instance)

@receiver(post_save, sender=MyModel)
def after_save(sender, instance, created, **kwargs):
    print('After saving:', instance, 'Created:', created)
This code listens for pre_save and post_save signals on MyModel and prints messages before and after saving.
Execution Table
StepSignal/EventInstance StateActionOutput
1Call save() on instanceInstance with dataTrigger pre_save signalPrint 'Before saving: <instance>'
2pre_save signalInstance with dataRun before_save handlerPrint message before DB save
3Save instance to DBInstance savedDatabase write operationNo output
4Trigger post_save signalInstance savedRun after_save handlerPrint 'After saving: <instance> Created: True/False'
5End of save()Instance savedReturn from save()No output
💡 Save process completes after post_save signal handler runs
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
instanceNew unsaved objectReady to saveSaved in DBSaved in DBSaved in DB
createdN/AN/AN/ATrue if new, False if updateTrue/False
Key Moments - 3 Insights
Why does pre_save run before the instance is saved to the database?
Because pre_save signal is designed to let you modify or check the instance before it is saved, as shown in execution_table step 2.
What does the 'created' parameter in post_save indicate?
'created' is True if the instance was newly created and saved, False if it was an update, as shown in execution_table step 4.
Can changes made in pre_save affect what is saved to the database?
Yes, because pre_save runs before saving, any changes to the instance in pre_save will be saved, as implied in the flow and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the instance actually saved to the database?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Action' column for the database write operation in execution_table row 3.
According to variable_tracker, what is the value of 'created' after step 4?
AAlways True
BAlways False
CTrue if new instance, False if update
DUndefined
💡 Hint
Look at the 'created' row in variable_tracker after step 4.
If you modify the instance in pre_save, when will those changes be saved?
AImmediately after pre_save signal finishes
BDuring the save operation in step 3
CAfter post_save signal
DThey won't be saved
💡 Hint
Refer to concept_flow and execution_table steps 2 and 3 about when saving happens.
Concept Snapshot
Django signals pre_save and post_save run around model saving.
pre_save runs before saving, allowing changes before DB write.
post_save runs after saving, useful for actions after save.
Use @receiver decorator to connect handlers.
'created' in post_save shows if instance is new or updated.
Full Transcript
When you save a Django model instance, the pre_save signal runs first. This lets you check or change the instance before it goes into the database. Then Django saves the instance to the database. After saving, the post_save signal runs. It tells you the save is done and whether the instance was new or updated. You connect functions to these signals using the @receiver decorator. This way, you can run code automatically before or after saving a model.