In Django, signals provide a powerful mechanism to trigger actions in response to changes in the database. Among the most common signals are pre_save, post_save, and post_delete. They allow you to execute custom logic before or after saving or deleting a model instance.
Signals are messages sent when certain actions occur in Django. When a model instance is saved or deleted, Django can emit signals that receiver functions can listen to and act upon.
The pre_save signal is sent just before a model’s save() method is called. It is useful for modifying data before it is stored in the database.
# Set default title before saving the book
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Book
@receiver(pre_save, sender=Book)
def update_book_title(sender, instance, **kwargs):
if not instance.title:
instance.title = "Untitled"
The post_save signal runs after a model instance has been saved. It is commonly used for sending notifications or creating related records.
# Send notification after a new book is created
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Book, Notification
@receiver(post_save, sender=Book)
def send_book_created_notification(sender, instance, created, **kwargs):
if created:
Notification.objects.create(
message=f"A book titled '{instance.title}' was added."
)
The post_delete signal is triggered after a model instance is removed from the database. It is useful for cleaning up related resources.
# Remove associated cover image after deleting the book
from django.db.models.signals import post_delete
from django.dispatch import receiver
from .models import Book
@receiver(post_delete, sender=Book)
def delete_book_cover(sender, instance, **kwargs):
if instance.cover:
instance.cover.delete()
Simulate saving a "Book" to see the order in which signals fire.
dispatch_uid to avoid duplicate signalsapps.py when possiblepre_savepost_savepost_delete