Using Django's built in signals and writing custom signals.

Reading Time : ~ .

Django has beautiful feature of signals which will record all the actions performed on particular model. In the current blog post we’ll learn how to use Django's built-in signals and how to  create custom signal

Using Django’s built in Signals:
Django has lot of built in signals like pre_save, post_save, pre_delete and post_delete and etc., For more information about Django's built-in signals visit Now we’ll learn how to use django’s pre_delete signal with a simple example. In the way we use pre_delete in the present blog post we can use other signals also in the same way. We have two models called Author and Book an their models are defined in as below.

# In
from django.db import models

class Author(models.Model):
    full_name = models.CharField(max_length=100)
    short_name = models.CharField(max_length=50)

class Book(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    content = model.TextField()
    status = models.CharField(max_length=10, default=”Drafted”)
    author_id = model.PositiveIntegerField(null=True)

In the above two models we are not having author as foreignKey to Book model, so by default when the Author gets deleted it won’t delete all the Books written by the author. This is the case where signals comes to picture, we can achieve this by using pre_delete or post_delete signals. For this we’ll write a receiver function which will be called on pre_delete of the author object. Write the following code in your

def remove_books(sender, instance, **kwargs):
    author_id =

pre_delete.connect(remove_books, sender=Article)

In the above snippet sender is the model name on which the pre_delete signal is called, in the current example it is Author model. Remove_books is the receiver function which will be called on delete of the Author object. It takes sender, instance(the Author instance which is called for delete) and any other keyword arguments.

Writing Custom Signals:
Now in this section we’ll learn, how to create custom signals using the same above example. Suppose if the author has to get an email when the Book status is changed to “Published”. For this we have to create a file called to create a custom signal. By default all the signals are django.dispatch.Signal instances.

# In
import django.dispatch
book_publised = django.dispatch.Signal(providing_args=["book", “author”])

Create which contains the receiver function which will be called when the signal is dispatched.

# In
from django.dispatch import receiver
from .signals import *

def send_mail_on_publish(sender, **kwargs):
    # contains the logic to send the email to author.

In the above snippet receiver is decorator which tells the book_published signal that send_mail_on_publish is the receiver function which will be called when the book_publisehd signal is dispatched.

We can dispatch signal anywhere as following.

book_published.send(sender=Book, book=<Book Instance>, user=<Author Instance>)
Note: The most important to be remembered is when we just call book_published.send(*****) it won’t hit the receiver function. To make the signal hit the receiver function is we have to import receiver in your app’s 

# In

import receivers

But from Django 1.9+ if we import receivers in, it will cause runtime error of Apps not ready. To avoid this issue we have to import this inside the ready function of your

# In

from django.apps import AppConfig
class LoadReceivers(AppConfig):
    name = testapp

    def ready(self):
        from . import receivers
    By Posted On

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Autocomplete with Django-Haystack and Elasticsearch with single letter querying. Ashwin Kumar

Django's haystack provides autocomplete functionality. To do autocomplete effectively, the search backend(elasticsearch in this case) uses n-grams (essentially a small window passed over the string). ...

Continue Reading...
Querying with Django Q objects Dinesh Deshmukh

Querying with Django Q objects: Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects ...

Continue Reading...
Integration of 2checkout with django Nikhila Mergu

Payment Gateways which facilitate communication within banks and Security is an integral component of all payment gateways, as sensitive data such as Credit Card Numbers ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties