假设以下型号:
class DeltaCheck(models.Model):
logs = generic.GenericRelation('Log')
title = models.CharField(max_length=50)
owner = models.ForeignKey(User)
class Log(models.Model):
title = models.CharField(max_length=50)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
如果我创建了DeltaCheck和几个Logs然后删除了DeltaCheck,那么日志也会被删除:
In [7]: Log.objects.count()
Out[7]: 10
In [8]: DeltaCheck.objects.get().delete()
In [9]: Log.objects.count()
Out[9]: 0
但如果我删除了用户(字段owner
),DeltaCheck
会被删除,但不会删除日志,请看:
In [14]: Log.objects.count()
Out[14]: 10
In [15]: DeltaCheck.objects.get().owner.delete()
In [16]: DeltaCheck.objects.all()
Out[16]: []
In [17]: Log.objects.count()
Out[17]: 10
为什么?看起来像一个错误。
编辑1:
奇怪的是,pre_delete和post_delete信号都会在失败的情况下被触发......中间会发生什么?
编辑2:
好的,这很奇怪。如果我为pre_delete
信号定义一个EMPTY接收器......它可以工作:/刚添加:
@receiver(pre_delete)
def recv(**kwargs):
pass
现在它有效......