Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
Django provides a set of built-in signals that let user code get notified by Django itself of certain actions. These include some useful notifications:
-
Sent before or after a model’s save() method is called.
-
Sent when a ManyToManyField on a model is changed.
-
Sent when Django starts or finishes an HTTP request.
In this i am showing u step by step process for sucessful
First step :
Create an Signals.py file in the folder where u wan to add the signals to models
second step;
There will be an init.py file in every folder of django .open it and type
import signals
third step open ur signals.py and add the following
# in models.py
from django.db.models.signals import post_save
from django.dispatch import receiver
// below example shows signal that is triggered after saving of model
@receiver(post_save, sender=< ur model name>)
def <function name>(sender, **kwargs):
# the object which is saved can be accessed via kwargs 'instance' key.
obj = kwargs['instance']
print obj.username
No comments:
Post a Comment