Working Ninja
Django Signals

This post outlines that bare minimum that needs to be done to set up a post_save signal using Django. One rationale behind using a signal is that a model instance can be saved without the user having to wait for additional code to process (e.g. consuming an external API). This lets the user quickly save a form and be on their merry way while in the background the model save() method triggers our signal which executes additional code. This all happens without incuring any wait time for the user.

Create a new file to hold our signals:

# polls/
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Polls

@receiver(post_save, sender=Polls)
def email_poll_author(sender, **kwargs):
    # Code to email poll author

""" """

Initialize our signals when Django loads:

# polls/
from django.apps import AppConfig

class PollsConfig(AppConfig):
    def ready(self):
        from . import signals

""" """

Lastly, tell Django to use our AppConfig (polls/

# polls/
default_app_config = 'polls.apps.PollsConfig'

""" """

Additional types of signals: