我正在使用django信号来跟踪网站上的用户操作(个人资料/订阅更新)。并拥有以下models.py:
class Customer(models.Model):
user = models.OneToOneField(to=User, null=True, blank=True)
...
class Subscription(models.Model):
customer = models.ForeignKey('Customer', null=True, related_name='subscriptions')
...
class Activity(models.Model)
source = models.ForeignKey('Customer', null=True, related_name='subscriptions')
...
并发出以下信号:
@receiver(post_delete, sender=Subscription, dispatch_uid='track_sub_post_delete')
def track_subscription_delete(sender, instance, *args, **kwargs):
extra = {'sub': instance,
'action': 'deleted',
'old': instance.__str__()}
tracker = ActivityTracker(target=instance.customer, mode='subscription', extra=extra)
tracker.save()
当我试图删除任何客户/用户时(on_delete = models.CASCADE):
客户被删除活动对象仍然有客户参考(已被删除)这导致django粉碎与smth 像
Exception Value: insert or update on table "dashboard_activity" violates foreign key constraint "smth_fk_accounts_source_id"
DETAIL: Key (source_id)=(1) is not present in table "dashboard_customers".
(目前无法找到确切的错误消息)
有什么方法可以阻止这种情况吗? (除了使用on_delete = SET_NULL并通过信号手动删除相关对象)