删除个人资料和公司后删除用户

问题描述 投票:0回答:1

我有以下模型。

class Profile(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE)
  bedrijf = models.ForeignKey(Bedrijf, on_delete=models.CASCADE)

class Bedrijf(models.Model):
  bedrijfsnaam = models.CharField(max_length=30, null=True, blank=True)

@receiver(post_save, sender=User)  
def create_user_profile(sender, instance, created, **kwargs):
  if created:
    n = Bedrijf.objects.create(bedrijfsnaam="Gaarne_bedrijfsnaam_instellen")
    n.save()
    Profile.objects.create(user=instance, bedrijf=n)

我想在删除Bedrijf后删除用户。使用此配置,将删除配置文件,但不会删除用户。解决方案是什么?

django foreign-keys one-to-one
1个回答
0
投票
我用这样的post_delete信号解决了它:

# Delete User after Profile (chained with Bedrijf) has been deleted @receiver(post_delete, sender=Profile) def delete_user_after_profile_delete(sender, instance, using, **kwargs): instance.user.delete()

© www.soinside.com 2019 - 2024. All rights reserved.