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

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
Latest Comments
Related Articles
Integration Of GitHub API with python django Nikhila Mergu

Using Github integration, we can get the user verified email id, general information, git hub URL, id, disk usage, public, private repo's, gists and followers, ...

Continue Reading...
How to index binary files in django haystack Ramya Ambati

Now we are going to index text content which is stored in structured files such as PDFs, Microsoft Office documents, images, etc using haystack and ...

Continue Reading...
django Payu Payment gateway Integration Shirisha Gaddi

In this blog, we will see how to integrate Django and PayU Payment Gateway. To integrate with PayU, we have package called "django-payu" - a ...

Continue Reading...