What is Signals in django ?

Signals are pieces of code which contain information about what is happening. A dispatcher is used to sending the signals and listen for those signals.
There are two key elements in the signals first one is senders and second one is receivers. As the name suggests, the sender is the one responsible to dispatch a signal, and the receiver is the one who will receive this signal and then do something.
The connection between the senders and the receivers is done through “signal dispatchers”, which are instances of Signal, via the connect method.

Example:--

from django.contrib.auth.models import User
from django.db.models.signals import post_save

def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

post_save.connect(save_user_profile, sender=User)

OR--

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

 

Total Views: 427