In this tutorial, we are going to learn…
You can watch the below video where I have explained all the points about Django Signals along with a demo. Or you can also keep continuing reading.
Do you want to call some function or do you want to perform some action after performing a Django event like submitting data into the Django Model?
You need a Django signal.
It is easy to achieve that with just a few lines of code.
You have to import two Python objects- called
receiver which are available in the Django signal and Django dispatch modules. These modules come preinstalled with Django, so you don’t have to install any Django or Python package.
Here we are calling the function
data_submmited every time when data is saved in the Django model.
from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save) def data_submitted(sender, instance, **kwargs): # Do your task. print("Form submiited")
If you look into the above code, we are using the decorator
@receiver() along with the
data_submiited() will be called for all the model data submission. This function is called with multiple arguments like
sender (model name),
instance (instance of the model), and some others like
using. You can just print
kwargs map data to check arguements as key-value pairs.
What if you want to call the function only after submitting specific model data?
It’s very much possible.
Assign that model name to the parameter
sender and pass it to the decorator
@receiver() as below.
from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=MyModel) def data_submitted(sender, instance, **kwargs): # Do your task. print("MyModel data submiited")
Now, the function
data_submiited will be called only after the model
MyModel data submission.
Note: You can define these functions in the
model.py. Or you can even write a separate file to capture all the Django signal events.
Let me give you an example. This will make it easy to understand.
I was working on the online CV builder which is developed using the Django framework. It includes multiple model forms for personal data, employment data, academic data, etc. I wanted to maintain the timestamp as last time when the CV is updated.
To add this feature, I’m using the
post_save signal which calls a function
cv_updated() when the user edits any CV form. Inside
cv_updated(), I’m just saving the timestamp which depicts the last CV update time.
You can check how to save form data to the Django model.
This is all about the Django signals example. Similar to these there can be many use cases where you can use the Django signals.
There are multiple types of events you can use with the Django signal. Here are four signal events that are very popular and useful. I’m also adding a one-liner to explain where you can use each of them.
This signal event works to call a function after saving data (after
save() method). It will be used even if the single field value is updated in the Django model.
This signal event works to call a function just before the calling
It works to call a function just after deleting the model instance (after
It works to call a function just before deleting the model instance (before
Any doubt? Let’s discuss this in the comment section below.