Introduction to Signals in Django
In this video, we are going to learn about signals in django.
Introduction
-
In Django, we have something called signals.
-
Using this signals, our django app can gets notified whenever a certain type of event gets occured in the entire django project.
-
Lets take an example. Suppose you want to send a notification to the user whenever an object gets updated in the database. In this case, either you can invoke the notification function in the django view itself or you can use signals. Using signals, you will define a receiver function that will listen to a certain event and when that event gets occurred that receiver function will send the notification.
Signals
-
In signals, there are basically three things which you need to consider - sender, signals and receiver.
-
A sender is basically a component that sends the signal. It is typically an object or a class responsible for initiating the action that triggers the signal.
-
Whereas, A receiver is a function that gets called when a signal is sent. Receivers are basically a components that respond to the signal by performing certain actions or executing specific code.
-
When a signal is sent, Django looks for receivers connected to that signal and executes their associated functions in a synchronous manner.
-
Basically, in django, a model sends four different types of sigals. They are pre_save, post_save, pre_delete, post_delete.
-
pre_save signal is sent just before a model's save() method is called. It provides an opportunity to perform any necessary operations or modifications on the model instance before it is saved to the database. This pre_save signal is useful for tasks such as auto-generating timestamps, populating fields based on certain conditions, or performing validations.
-
post_save signal is sent just after a model's save() method is successfully executed and the model instance is saved to the database. It allows you to perform actions that need to occur after the model has been saved, such as sending notifications, updating related records, or triggering other processes based on the saved data.
-
pre_delete signal is sent just before a model's delete() method is called. It provides an opportunity to perform any necessary operations or cleanup tasks before the model instance is deleted from the database. This signal can be useful for tasks such as archiving related data, updating counters, or releasing resources associated with the model.
-
post_delete: This signal is sent just after a model's delete() method is successfully executed and the model instance is deleted from the database. It allows you to perform actions that need to occur after the model has been deleted, such as updating related records, triggering cascading deletions, or logging the deletion event.
Fixing problem in the profile update
-
In the last video, we dicussed about an issue during the profile update.
-
Whenever we update the profile image, the old image doesn't gets deleted automatically.
-
In this case, we can use signals to automatically delete the image whenever there is a profile image update operation.
-
So, what we will do, we will create a receiver that will respond to the pre_save signal sent by User model whenever there is a profile update operation.
-
We will learn how to write this type of code using signals in the next video.