Django Signals, Implementing On Save Listner
am having difficulties understanding how signals works, I went through some pages but none of them helped me get the picture. I have two models, I would like to create a signal th
Solution 1:
With the code below you can connect the save of an Instance object, with the after_save_instance_handler method. In this method you create the relation to the Audit. Please also see the generic relations doc
I usually add the signals in the models.py where the sender has been defined. Not sure if this is needed.
from django.db.models.signals import post_save
#####SIGNALS######defafter_save_instance_handler(sender, **kwargs):
#get the saved instance
instance_object = kwargs['instance']
#get the needed data
the_date = ...
the_user = ...
the_object_id = ...
#create the relation to the audit object
instance_object.audit_obj.create(operation="op963",operation_at=the_date,operation_by=the_user,object_id=the_object_id)
#connect the handler with the post save signal - django 1.1 + 1.2
post_save.connect(after_save_instance_handler, sender=Instances)
in django development version they added a decorator to connect the signal. thus instead of the call above you have to add this decorator to the handler
@receiver(post_save, sender=Instances)defafter_save_instance_handler(sender, **kwargs):
Post a Comment for "Django Signals, Implementing On Save Listner"