Nitin Raturi
Nitin Raturi's Software Engineering Blog

Follow

Nitin Raturi's Software Engineering Blog

Follow
Creating Signals in Django: An Introduction

Photo by Chinh Le Duc on Unsplash

Creating Signals in Django: An Introduction

Nitin Raturi's photo
Nitin Raturi
ยทJul 10, 2019ยท

5 min read

Django is a powerful web framework that provides developers with a wide range of tools to build web applications quickly and efficiently. One of the most useful features of Django is the ability to create signals, which allow developers to respond to events that occur within their applications.

In this article, we will discuss what signals are, how to create them, and some examples of how they can be used in Django applications.

What Are Signals?

Signals are a way for different parts of a Django application to communicate with each other. They allow one part of the application to send a message, or signal, to another part of the application when a particular event occurs.

Signals can be used to trigger actions when certain events happen within the application, such as when a user logs in or when a new record is added to the database. They can also be used to implement custom functionality, such as sending notifications or logging events.

Creating Signals in Django

To create a signal in Django, we need to define a sender and a receiver. The sender is the part of the application that sends the signal, while the receiver is the part of the application that receives the signal and responds to it.

Defining a Signal

To define a signal in Django, we first need to import the Signal class from the django.dispatch module. We can then create an instance of the Signal class, which will represent our signal.

from django.dispatch import Signal

my_signal = Signal()

In this example, we have created a signal called my_signal.

Connecting a Receiver

Once we have defined our signal, we need to connect a receiver to it. A receiver is a function that will be called when the signal is sent.

To connect a receiver to a signal, we use the receiver decorator provided by Django. The receiver decorator takes two arguments: the signal to which we want to connect the receiver, and the sender of the signal.

from django.dispatch import receiver

@receiver(my_signal, sender=MyModel)
def my_receiver(sender, **kwargs):
    # do something here

In this example, we have defined a receiver function called my_receiver. We have used the @receiver decorator to connect this function to our my_signal signal, and we have specified that the sender of the signal should be the MyModel class.

Sending a Signal

Now that we have defined our signal and connected a receiver to it, we can send the signal. To send a signal, we simply call the signal object and pass any data that we want to send with the signal.

my_signal.send(sender=MyModel, data="Hello, World!")

In this example, we are sending our my_signal signal and specifying that the sender of the signal is the MyModel class. We are also passing some data with the signal, which will be available to the receiver function when it is called.

Examples of Using Signals in Django

Now that we know how to create and use signals in Django, let's take a look at some examples of how they can be used in practice.

Logging User Actions

One common use case for signals is logging user actions. For example, we might want to log when a user logs in or logs out of our application.

To do this, we can create a signal that is sent when a user logs in or logs out. We can then connect a receiver function that will log the event to our application's logging system.

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver

@receiver(user_logged_in)
def log_user_logged_in(sender, request

Django Built-In Signals

Here are some examples of built-in signals that Django provides:

pre_save and post_save

The pre_save and post_save signals are sent before and after a model instance is saved to the database, respectively.

from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from myapp.models import MyModel

@receiver(pre_save, sender=MyModel)
def my_pre_save(sender, **kwargs):
    # do something before MyModel is saved

@receiver(post_save, sender=MyModel)
def my_post_save(sender, **kwargs):
    # do something after MyModel is saved

In this example, we are defining two receiver functions, one for the pre_save signal and one for the post_save signal. Both of these functions are connected to the MyModel sender. The my_pre_save function will be called before the MyModel instance is saved to the database, and the my_post_save function will be called after the instance is saved.

pre_delete and post_delete

The pre_delete and post_delete signals are sent before and after a model instance is deleted from the database, respectively.

from django.db.models.signals import pre_delete, post_delete
from django.dispatch import receiver
from myapp.models import MyModel

@receiver(pre_delete, sender=MyModel)
def my_pre_delete(sender, **kwargs):
    # do something before MyModel is deleted

@receiver(post_delete, sender=MyModel)
def my_post_delete(sender, **kwargs):
    # do something after MyModel is deleted

In this example, we are defining two receiver functions, one for the pre_delete signal and one for the post_delete signal. Both of these functions are connected to the MyModel sender. The my_pre_delete function will be called before the MyModel instance is deleted from the database, and the my_post_delete function will be called after the instance is deleted.

user_logged_in and user_logged_out

The user_logged_in and user_logged_out signals are sent when a user logs in or logs out of the application, respectively.

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver

@receiver(user_logged_in)
def log_user_logged_in(sender, request, user, **kwargs):
    # log the user login event

@receiver(user_logged_out)
def log_user_logged_out(sender, request, user, **kwargs):
    # log the user logout event

Did you find this article valuable?

Support Nitin Raturi by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this