Django Signals: Model Event Hooks & Decoupled Architecture

Django includes a "signal dispatcher" that helps decoupled applications get notified when framework-level actions occur. The most widely used signals are model signals: post_save, pre_save, post_delete, and pre_delete.

The Classic Pattern: Auto-Creating User Profiles on Registration

Whenever a new User instance is created, we want to automatically generate a corresponding AuthorProfile record without cluttering our registration views:

# accounts/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from blog.models import AuthorProfile

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    """Listens for new user creation and initializes empty profile."""
    if created:
        # Create profile record for new user
        AuthorProfile.objects.create(user=instance)
    else:
        # Save profile if existing user is modified
        if hasattr(instance, 'profile'):
            instance.profile.save()

Registering Signals in apps.py ready()

Signals must be imported inside your AppConfig.ready() method so Django connects receivers at boot time:

# accounts/apps.py
from django.apps import AppConfig

class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'accounts'

    def ready(self):
        # Connect signal receivers
        import accounts.signals

Signal Anti-Pattern Warning

Do not overuse signals! Overusing signals makes code hard to trace, debug, and test because side effects occur "magically" behind the scenes. If logic is strictly internal to a model, explicitly override save() on the model instead.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is a Django Signal?

A Django Signal is an implementation of the Observer design pattern. It allows decoupled applications to be notified when actions occur elsewhere in the framework (e.g. notifying a profile app whenever a user record is saved in the auth app).

Q2. What is the difference between pre_save and post_save?

`pre_save` is sent just before a model's `save()` method completes; the record does not yet have a primary key if it is newly created. `post_save` is sent immediately after the record is committed to the database; it passes a boolean `created` parameter indicating whether this was an insert or an update.

Q3. How do you prevent infinite recursion in a post_save signal?

If you call `instance.save()` inside a `post_save` receiver, it triggers the signal again, causing an infinite recursion crash. To prevent this, either update using `update_fields` with a guard condition, use `Model.objects.filter(id=instance.id).update(...)`, or perform modifications in `pre_save` before commit.

Q4. When should you use signals vs overriding model save()?

Use model `save()` for business logic that directly pertains to that specific model (e.g. generating a slug from a title). Use signals when decoupled external apps need to react to an event without creating circular import dependencies (e.g. creating an audit log or sending an email).

Explore Related Django Tutorials & Next Steps

Broaden your backend architecture, REST API design, and production engineering expertise:

Django Models & Field Types

Hook into model lifecycle events with post_save and post_delete.

Celery & Background Tasks

Dispatch asynchronous jobs from signals without blocking HTTP threads.

Custom User Model (AbstractUser)

Auto-create user profile records whenever a new User instance is registered.

130+ Django Interview Questions

Discuss signals vs overriding model save() in technical interviews.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!