Interview Questions

Python -Django Signal handlers

Use the following code to handle any events/signals in Django.

from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import Device

@receiver(post_save, sender=Device)
def my_handler(sender, **kwargs):
        print "myhandler"
        # your code for processing

In the above code , my_handler method handles post_save event/signal , which will be called whenever a save event is triggered in the Device model. Similarly other signals can be imported and specified in the @receiver annotation.