What is Django Signals | Django Tutorial for Beginners

Django Signals copy

What is Django Signals | Django Tutorial for Beginners

What is Django Signals

In this article, we are learning about Django signals. You will learn all about Signals in this Django tutorial, it will be beneficial for you if you read this lesson from start to end, otherwise, you can skip this tutorial, and go to another.

At the start, we want to understand what is Django signal is actually? If I shared some background knowledge to make a strong concept to understand signals. It is true that you have listened to the name signal. When any signal comes, it means, some other action will be performed. For example on the traffic line, when the red light signal is on it means you cannot drive your motor, you have to wait until it turned to yellow.

Another example, when you send a love signal to your friend, your friend understood that what I have to do here, maybe a response similar to love or the opposite.

So you have understood that when any signal is generated or produce or developed there will be an action that performs.

So Django signal is the signal is linked with action when an event occurs. When an event occurs (signal) then action will be called. Signal will be associate with the action.

If you are working with Django signals, then you should have to remember three points, it will help you to understand signals working and clearing concepts.

  • Sender     : It is the model class that sends the signal, I mean when a new record is inserted or updated
  • Signal      : It is the actually signal, like pre_save(), post_save() etc
  • Receiver  : It is the receiver function that will be called when the signal is a product, a specific signal should be linked with this receiver function.

There are different types of signals, you should know if you are learning Django.

  • Login and Logout
  • Test purposes signals
  • Models Based
  • Database signal
  • Request & Response
  • Management signal
  • Custome signal

Why do we need to create Django Signals

There are different causes, for that we need to create a Django Signals, it depends upon our requirements. For example, if you want to create a profile for all users automatically when they sign up, you can use signals. When a user creates its account using the Django auth framework, after creating its account a new entry will be created in the profile model for a user that created the right now account. It makes professional one way.

There are other more examples, in which we take advantage of signals. When you are want to run any code when the save method is called (save method is called when you enter a new entry in the database table, and save method saves the data in the database table). There are two types of signals related to the saving method.

pre_save(): It will be called at the beginning of this method when it is calling

post_save(): It will be called at the end of this method when it is calling

There is another method that works of deleting a record in a table when you want to delete a tuple from the database table, then the delete method is called, so it has also two types of signals:

pre_delete(): It will be called at the beginning of this method when it is calling

post_delete(): It will be called at the end of this method when it is calling

Before creating Django Signals?

If you want o to create a signal, you have to keep some points in your cool mind. These points are common for any type of signal.

You should have to find what is the sender? Actually most of the time, it may be a model class.

The next thing is that you have to think about the signal, what type of signal we need, or when we want to run a code after which type of signal calling. For example, the signal may be post_save(), etc.

The very important point is that, the receiver function, which function you want to call when the post_save() signal is produced.

At the last, you need to connect your receiver function with the signal, because when the sender sends the signal, which receiver should have to receive it? For this reason, you need to connect the receiver function with a signal.

There are two ways to connect signal with receiver function which you want to call after coming signal

  • Manual connector
  • Decorator

Code Example to create Django Signal

In this section we are going to creating a signal, this is a very simple example, but it will be beneficial for you to work with standard websites like e-commerce also. Actually, we will want to run a function when a signal comes. The signal which comes will be a model class. So, each and every step I will share with you!

Model 1: User

In this case, we are getting a model class that is the Django default Model, when a new entry is inserted in the User model, it means an event is occurs, so it means a signal is created. So when such an event is created, then action will be performed. That will be discussed in the next steps.

Model 2 : User_Profile

Now, you have to create another model, which you want to automatically enter insertion for a specific user, that was created in the first step. How automatically entry insertion in profile model, you have to use Django signals,. There are different types of signals, you can use, but the post_save() signal is suitable for this.


You need to create a file signals.py file in your application folder, you have to write a code for creating a signal.

from django.db.models.signals import post_save
from django.dispatch import receiver
from . models import User_profile
from users.models import User

# Using Django Signals, we are creating a profile automatically for all users who create their accounts using the Django user model class

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, created, **kwarg):
    if created:
        User_profile.objects.create(user = instance)

Register signal

Now, you have to open the apps.py file, you need to write some code for signal registration

class UsersConfig(AppConfig):
    name = ‘users’
    def ready(self): 
        import application_name.signals # write your application name, in which you have created signals file

Add one line in __init__.py

You have to open __init__.py file in your application folder, add the following line of code, but replace application_name to your application name

default_app_config = ‘Application_name.apps.Application_nameConfig’

Now! what you have to do?

As you have understood this article about “Django Signals”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.

On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!

If this article is good then share it on FacebookTwitterPinterestInstagram, etc.

Advanced Tutorials for Beginners

What is Django Model Manager | Django Tutorials

Django Model Relationship and its Types | Django Tutorials

What is Django Query Set API | Django Tutorial for Beginners

What is Django URL Tag in Django Template Language

Full Guide Function-based views Django | Complete Django Tutorials

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar

Get your Enrollment


Leave your details below and receive a discount coupon in your inbox