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?
✗ Incorrect
The
pre_save signal is sent just before saving a model instance.What does the
created argument in a post_save signal handler indicate?✗ Incorrect
The
created argument is True if the instance was created, False if updated.How can you connect a function to a Django signal?
✗ Incorrect
You connect signal handlers using the
@receiver decorator or the connect() method.Which signal would you use to perform an action after a model instance is saved?
✗ Incorrect
The
post_save signal runs after the instance is saved.What is a benefit of using Django signals like
pre_save and post_save?✗ Incorrect
Signals let different parts of your app respond to events without tightly coupling code.
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.