在django中使用Pre_delete信号 [英] Using Pre_delete Signal in django

查看:797
本文介绍了在django中使用Pre_delete信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想要跟踪所有被删除的问题。所以我在我的模型文件中创建了一个类(表)。

  class Deleted(models.Model):
question = models.IntegerField(null = True,blank = True)正在删除的问题的#id
user = models.IntegerField(null = True,blank = True)#id的用户删除问题
dt = models.DateTimeField(null = True,blank = True)#time问题被删除

当用户尝试删除问题时,此删除功能将被调用:

  def delete_questions(请求,用户,问题):
在问题中的q:
q.delete()

我的疑问是如何我做一个django的pre_delete信号来填充我创建的新表。



〜newbie尝试高级任务〜
感谢提前:)

解决方案

您首先定义 receiver 您要使用的:

  def log_deleted_question(发件人,实例,使用,** kwargs):
d = Deleted()
d.question = instance.id
d.dt = datetime.datetime.now()#考虑在您的删除定义中使用auto_now = True
#不知道你会通过信号获取用户
#,因为它可能发生在许多地方(如命令行)
d.save()

然后定义您的接收器装饰器:

 从django.db。 model.signals从django.dispatch导入接收器导入pre_delete 


@receiver(pre_delete,sender = Question,dispatch_uid ='question_delete_log')
/ pre>

总共添加:

 从django.db.models.signals导入pre_delete 
从django.dispatch import receiver

@receiver(pre_delete,sender = Question,dispatch_uid ='question_delete_signal')
def log_deleted_question(发件人,实例,使用,** kwargs):
d =删除()
d.question = instance.id
d.dt = datetime.datetime.now()
d.save()

您可以将此函数放在您的 models.py 文件中,因为您知道它将被正确加载和连接。



问题是,您没有让用户请求删除。由于可以从没有与其相关联的请求的django api(命令行,shell等)触发删除。因此,您可能希望避免使用信号,如果绝对关键的是将用户与删除一起存储。


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(models.Model):
question = models.IntegerField(null=True, blank=True)#id of question being deleted
user = models.IntegerField(null=True, blank=True)#id of user deleting the question
dt = models.DateTimeField(null=True, blank=True)#time question is deleted

When a user tries to delete a question This delete function is called:

def delete_questions(request, user, questions):
  for q in questions:
        q.delete()

My doubt is how can i make a pre_delete signal of django to populate the new table I have created.

~newbie trying hefty task~ Thanks in advance:)

解决方案

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.

这篇关于在django中使用Pre_delete信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆