Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using pre_delete and post_delete Signals in Django
📖 Scenario: You are building a simple Django app to manage a list of books. You want to perform actions right before and right after a book is deleted from the database.
🎯 Goal: Create a Django model for books and use pre_delete and post_delete signals to print messages when a book is about to be deleted and after it has been deleted.
📋 What You'll Learn
Create a Django model called Book with a title field
Import and connect pre_delete and post_delete signals
Write signal handler functions named before_book_delete and after_book_delete
Connect the signal handlers to the Book model
Use print() statements inside handlers to show messages
💡 Why This Matters
🌍 Real World
In real apps, signals help run code automatically when database changes happen, like cleaning up related data or logging actions.
💼 Career
Understanding Django signals is important for backend developers to manage side effects and keep data consistent in web applications.
Progress0 / 4 steps
1
Create the Book model
Create a Django model called Book with a single field title that is a CharField with max length 100.
Django
Hint
Use models.CharField(max_length=100) for the title field inside the Book class.
2
Import signals and define handler functions
Import pre_delete and post_delete from django.db.models.signals. Define two functions: before_book_delete and after_book_delete that take sender, instance, and **kwargs as parameters. Inside each function, add a print() statement with messages "About to delete: " plus the book title, and "Deleted: " plus the book title, respectively.
Django
Hint
Remember to use f-strings to include the book title in the print messages.
3
Connect the signal handlers to the Book model
Use the connect() method on pre_delete and post_delete to connect before_book_delete and after_book_delete functions to the Book model.
Django
Hint
Use pre_delete.connect(your_function, sender=Book) to connect the signal.
4
Complete the Django app setup
Add the necessary import for apps.py to ensure signals are registered when the app is ready. Import the signals module inside the ready() method of your app config class named BooksConfig.
Django
Hint
Import the signals module inside the ready() method to register signal handlers.
Practice
(1/5)
1. What is the main difference between Django's pre_delete and post_delete signals?
easy
A. pre_delete runs after a record is deleted, post_delete runs before.
B. pre_delete runs before a record is deleted, post_delete runs after.
C. Both signals run at the same time during deletion.
D. pre_delete only works with models, post_delete only with forms.
Solution
Step 1: Understand signal timing
pre_delete is triggered just before a model instance is deleted from the database.
Step 2: Understand post_delete timing
post_delete is triggered immediately after the instance has been deleted.
Final Answer:
pre_delete runs before a record is deleted, post_delete runs after. -> Option B
Quick Check:
Signal timing difference = pre_delete runs before a record is deleted, post_delete runs after. [OK]
Hint: Remember: pre_delete before removal, post_delete after removal [OK]
Common Mistakes:
Confusing the order of signal execution
Thinking both signals run simultaneously
Assuming signals work only with forms
2. Which of the following is the correct way to connect a pre_delete signal to a model named Book?
easy
A. Book.pre_delete.connect(my_handler)
B. post_delete.connect(my_handler, sender=Book)
C. pre_delete.connect(my_handler, sender=Book)
D. connect(pre_delete, my_handler, Book)
Solution
Step 1: Recall signal connection syntax
In Django, signals are connected using signal.connect(handler, sender=Model).
Step 2: Apply to pre_delete and Book
Use pre_delete.connect(my_handler, sender=Book) to connect the handler to the Book model.
Final Answer:
pre_delete.connect(my_handler, sender=Book) -> Option C