在Django中,post_delete在删除之前是如何触发的?
I am seeing post_delete
fire on a model before the instance is actually deleted from the database, which is contrary to https://docs.djangoproject.com/en/1.6/ref/signals/#post-delete
我看到post_delete在实例实际从数据库中删除之前启动了一个模型,这与https://docs.djangoproject.com/en/1.6/ref/signals/#post-delete相反
Note that the object will no longer be in the database, so be very careful what you do with this instance.
注意,对象将不再位于数据库中,所以要非常小心地处理这个实例。
If I look in the database, the record remains, if I requery using the ORM, the record is returned, and is equivalent to the instance:
如果我在数据库中查找,记录仍然保留,如果我使用ORM进行请求,则返回记录,并与实例等效:
>>> instance.__class__.objects.get(pk=instance.pk) == instance
True
I don't have much relevant code to show, my signal looks like this:
我没有太多相关的代码可以显示,我的信号是这样的:
from django.db.models.signals import post_delete, post_save
@receiver(post_delete, sender=UserInvite)
def invite_delete_action(sender, instance, **kwargs):
raise Exception(instance.__class__.objects.get(pk=instance.pk) == instance)
- I am deleting this instance directly, it's not a relation of something else that is being deleted
- 我正在直接删除这个实例,它不是要删除的其他东西的关系
- My model is pretty normal looking
- 我的模特看上去很普通
- My view is a generic DeleteView
- 我的视图是一个通用的删除视图
- I haven't found any transactional decorators anywhere - which was my first thought as to how it might be happening
- 我在任何地方都没有发现任何事务修饰符——这是我对它如何发生的第一个想法
Any thoughts on where I would start debugging how on earth this happening? Is anyone aware of this as a known bug, I can't find any tickets describing any behaviour like this – also I am sure this works as expected in various other places in my application that are seemingly unaffected.
有没有想过我该从哪里开始调试这到底是怎么发生的?是否有人意识到这是一个已知的bug,我找不到任何描述这种行为的罚单——而且我确信,在我的应用程序中其他似乎不受影响的地方,这种方法也能正常工作。
If I allow the execution to continue the instance does end up deleted... so it's not like it's present because it's failing to delete it (pretty sure post_delete shouldn't fire in that case anyway).
如果我允许执行继续,实例最终会被删除……所以它不会出现,因为它没有删除它(非常确定post_delete在这种情况下不应该触发)。
1 个解决方案
#1
1
I believe what I am seeing is because of Django's default transactional behaviour, where the changes are not committed until the request is complete.
我认为我看到的是Django的默认事务行为,在请求完成之前不会提交更改。
I don't really have a solution – I can't see a way to interrogate the state an instance or record would be in once the transaction is completed (or even a way to have any visibility of the transaction) nor any easy way to prevent this behaviour without significantly altering the way the application runs.
我真的没有一个解决方案——我看不出一种审问国家实例或者记录会在交易完成后(甚至有任何事务)的可见性的方法,也不是任何简单的方法来防止这种行为没有显著改变应用程序运行的方式。
I am opting for ignore the problem for now, and not worrying about the repercussions in my use-case, which in fact, aren't that severe – I do welcome any and all suggestions regarding how to handle this properly however.
我选择暂时忽略这个问题,而不用担心我的用例的影响,事实上,我的用例并没有那么严重——我欢迎任何和所有关于如何正确处理这个问题的建议。
I fire a more generic signal for activity logging in my post_delete, and in the listener for that I need to be able to check if the instance is being deleted – otherwise it binds a bad GenericRelation referencing a pk that does not exist, what I intended to do is nullify it if I see the relation is being deleted - but as described, I can't tell at this point, unless I was to pass an extra argument whenever I fire the signal inside the post_delete.
我火一个更通用的信号活动日志我post_delete和侦听器,我需要检查实例是否被删除,否则它将一个坏GenericRelation引用不存在的pk,我要做的是取消它如果我看到描述的关系被删除,但是,我不能告诉在这一点上,除非我是通过一个额外的参数每当我火post_delete内的信号。
更多相关文章
- 【实例】python re 正则表达式 同时选择带有“是”和“的”句子
- Python数据挖掘实例(实时更新)
- 使用Python编写简单的端口扫描器的实例分享【转】
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的sh
- Linux编程之《只运行一个实例》
- Linux Kernel 学习笔记8:同步与互斥之信号量
- Linux下的find 命令使用实例
- 守护进程的单实例实现
- Linux系统编程——进程间通信:信号中断处理