You start by defining the receiver you want to use:
def log_deleted_question(sender, instance, using, **kwargs): d = Deleted() d.question = instance.id d.dt = datetime.datetime.now()
Then define the receiver decoder:
from django.db.models.signals import pre_delete from django.dispatch import receiver @receiver(pre_delete, sender=Question, dispatch_uid='question_delete_log')
Add it at all:
from django.db.models.signals import pre_delete from django.dispatch import receiver @receiver(pre_delete, sender=Question, dispatch_uid='question_delete_signal') def log_deleted_question(sender, instance, using, **kwargs): d = Deleted() d.question = instance.id d.dt = datetime.datetime.now() d.save()
You can put this function in your models.py file, since you know that it will be loaded and properly connected.
The problem is that you are not asking the user to delete. Because deletion can be called from django api (command line, shell, etc.), which does not have a request associated with it. For this reason, you can avoid using signals if it is absolutely important that you save the user along with the deletion.
Josh Smeaton
source share