Using Pre_delete Signal in django

前端 未结 1 2042
情话喂你
情话喂你 2021-02-05 18:10

In my app I want to keep a track of all the questions that are being deleted. And so I have created a class(table) as such in my models file.

class Deleted(model         


        
相关标签:
1条回答
  • 2021-02-05 18:43

    You start off 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() # consider using auto_now=True in your Deleted definition
        # not sure how you'd get the user via a signal, 
        # since it can happen from a number of places (like the command line)
        d.save()
    

    Then define your receiver decorator:

    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 altogether:

    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, as you know it'll be loaded and connected up correctly.

    The problem though, is that you don't get the user requesting the delete. Since a delete can be triggered from the django api (command line, shell, etc), which doesn't have a request associated with it. For this reason, you might want to avoid using signals if it's absolutely critical that you store the user along with the delete.

    0 讨论(0)
提交回复
热议问题