Signals In Django

Aakash Verma
Analytics Vidhya
Published in
3 min readMar 8, 2021

In this article, We will discuss what Signals are in Django and how we can use it in our Django project. so let’s start.

In many cases when there is some modification in model instance or creation of a particular model instance we may need to execute some action. Django Provides an elegant way to handle such situations. The signals are utitlities that allow us to associate events with actions.

First Let’s go over Basic Concepts of signals,

The Signal :

A signal is an object corresponding to particular event.

from django.dispatch import Signal  make_order = Signal(providing_args=["toppings", "size", "type"])

Signals can send messages on occasion of a particular event. This is achieved by calling send() method on the signal instance (passing in a sender argument, along with the arguments specified above):

class Burger:     
def create_order(self, toppings, size, type):
make_order.send(sender=self.__class__, toppings=toppings, size=size, type=type)

The Receiver :

Reciever are python functions connected to each signal. when the signal sends a message, each connected reciever get’s called. Receivers’ function signatures should match what the signal’s send() method uses. You connect a receiver to a signal using the @receiver decorator.

from django.dispatch import receiver 
from burger import signals
@receiver(signals.pizza_done)
def notify-when_order_created(sender, toppings, size, **kwargs):
# Create an order

Above was the basics of signal Dispatcher. Receiver are connected to signals and received messages sent by signal and perform further

I will demonstrate you the most common use-case of signals i.e. to create a profile instance when user instance gets created.

The most used signals are :-

pre_save/post_save: — Sent before or after a model’s save() method is called.

pre_delete/post_delete: — Sent before or after a model’s delete() method or queryset’s delete() method is called.

Explaination :

First we imported post_save and receiver, then we created a receiver function create_user_profile which will receive a post_save signal. we are using receiver decorator to call the create_user_profile function on creation of user instance.

Hence we accept sender, instance, created(this will be a boolean stating whether the User instance has been created or not ) parameters on create_user_profile function,

and upon truthy value of created we create the Profile object for the user.

Above stated is the one way to implement signals, you can also have the code for receiver function in a different file and use it (Note: you have to import the file properly in your app’s app.py ready method, else it won’t work.

Signals are pretty handy, you see signals are synchronous, means normal program execution flow runs each receiver in turn before continuing with the code that sent the signal. You can read more about signals in Django Docs.

The signal dispatcher mechanism is not special to Django, but is actually a well known design pattern: the Observer pattern. The only difference is in terminology: in the Observer pattern a signal is known as a ‘subject’, and a receiver as an ‘observer’.

If you face any difficulties implementing signals, please let me know in the comments.

Happy Coding !!!

--

--

Aakash Verma
Analytics Vidhya

Computer Science Undergrad ,Tech Enthusiast , Loves writing about Technology