Using Django pre_save and post_save Signals
📖 Scenario: You are building a simple Django app to manage books in a library. You want to automatically update a timestamp before saving a book and log a message after saving it.
🎯 Goal: Create a Django model for Book and use pre_save and post_save signals to update a field and print a message.
📋 What You'll Learn
Create a
Book model with fields title (string) and last_updated (DateTimeField).Create a
pre_save signal handler that updates last_updated to the current time before saving.Create a
post_save signal handler that prints 'Book saved!' after saving.Connect the signal handlers properly to the
Book model.💡 Why This Matters
🌍 Real World
Automatically updating timestamps and triggering actions on model changes is common in web apps for data integrity and notifications.
💼 Career
Understanding Django signals is important for backend developers to handle side effects and keep code clean and modular.
Progress0 / 4 steps