0
0
Djangoframework~5 mins

pre_save and post_save signals in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the pre_save signal in Django?
The pre_save signal is sent just before a model's save() method is called. It allows you to run code or modify data before the object is saved to the database.
Click to reveal answer
beginner
When is the post_save signal triggered in Django?
The post_save signal is triggered immediately after a model instance has been saved to the database. It is useful for actions that depend on the object being saved.
Click to reveal answer
intermediate
How do you connect a function to the pre_save signal for a Django model?
You use the @receiver(pre_save, sender=YourModel) decorator or pre_save.connect(your_function, sender=YourModel) to link your function to the signal.
Click to reveal answer
intermediate
What arguments does a signal handler for post_save receive?
A post_save handler receives sender, instance (the saved object), created (True if new object), update_fields, and kwargs.
Click to reveal answer
advanced
Why might you use pre_save instead of overriding the save() method?
Using pre_save keeps your code separate from the model logic, making it easier to maintain and reuse. It also allows multiple functions to listen to the save event without changing the model.
Click to reveal answer
Which signal is sent before a Django model instance is saved?
Apre_delete
Bpre_save
Cpost_save
Dpost_delete
What does the created argument in a post_save signal handler indicate?
AIf the instance was updated
BIf the save was successful
CIf the instance was deleted
DIf the instance was newly created
How can you connect a function to a Django signal?
ABy overriding the model's <code>save()</code> method
BBy importing the signal in views.py
CUsing <code>@receiver</code> decorator or <code>signal.connect()</code>
DBy calling the function manually
Which signal would you use to perform an action after a model instance is saved?
Apost_save
Bpre_save
Cpre_delete
Dpost_init
What is a benefit of using Django signals like pre_save and post_save?
AThey allow decoupled code to react to model events
BThey automatically create admin pages
CThey speed up database queries
DThey replace the need for models
Explain how pre_save and post_save signals work in Django and when you might use each.
Think about what happens before and after saving a model.
You got /5 concepts.
    Describe how to connect a function to a Django model's post_save signal and what arguments the function receives.
    Focus on the connection method and handler parameters.
    You got /3 concepts.