我有以下模型。
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后删除用户。使用此配置,将删除配置文件,但不会删除用户。解决方案是什么?
# 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()