Signals In Django – Usage with examples
Suppose whenever a user registers himself to a given website, we want the user profile to be automatically created. Or consider when a user profile is deleted, we want to notify the admin about the same. In this case, Django in Python provides signals to carry out the above-mentioned tasks on its own without having to write any code manually. Therefore, in this tutorial, we will learn about Signals in Django.
What are Django Signals?
Signals are events that occur after performing an action on the database. In other words, if we modify the models by creating or deleting entries in the database, the signals can call or invoke any specific function.
There are three types of signals that are provided by Django:
- pre_save/post_save: These signals will invoke a function either before the instance of a model is created or after the instance of the model is created.
- pre_delete/post_delete: These signals will invoke a function either before the instance of a model is deleted or after the instance of the model is deleted.
- pre_init/post_init: These signals invoke a function after instantiating a model, for example, using the
__init__()
function.
Using the post_save signal on a model
Let us consider an example model named ‘Profile’ which will store basic information about the user such as name, username, email, and location. The following code represents the Profile Model.
class Profile(models.Model): name= models.CharField(max_length=200,blank=True,null=True) email=models.EmailField(max_length=500,blank=True,null=True) username=models.CharField(max_length=200,blank=True,null=True) location=models.CharField(max_length=200,blank=True,null=True) def __str__(self): return str(self.name)
To use the Django signals we will initially import the required modules using the following code:
from django.db.models.signals import post_save,post_delete from django.dispatch import receiver
Django signals comprise senders and receivers. Senders notify receivers that some action has taken place. We use the receiver decorator here as the code becomes simplified by extending or modifying the behavior of the predefined receiver module in Django.
Next, we will create the function that will be triggered when some event takes place and connect it with the sender instance model.
@receiver(post_save,sender=Profile) def createProfile(sender,instance, created, **kwargs): print('Profile created')
We have connected the post_save
signal to the sender, which in this case is the Profile model. Anytime a profile is saved in the database, the createProfile()
function is invoked which prints the message ‘Profile created’.
The parameters in the createProfile()
function are:-
- sender:- The model which will send the signal to the receiver.
- created:- This checks whether any new instance is created in the model
- **kwargs:- This represents any extra keyword arguments
Output:
Using the post_delete signal on a model
Similarly to invoke an event after deleting an instance of a model we use the post_delete signal. We have connected the post_delete signal to the sender, which in this case is the Profile model. Whenever a profile is deleted in the database, the deleteProfile() function is invoked which prints the message ‘Deleted Profile successfully’.
@receiver(post_delete,sender=Profile) def deleteUProfile(sender,instance, **kwargs): print('Instance:',instance) print('Profile deleted')
Output:
Thus we reached the end of this tutorial on introduction to signals in Django. To learn more about Django in Python click on the following link: Add CSS and Static Files in Django
Leave a Reply