因此,我正在使用Django创建一个社交媒体应用程序,并且我已经与用户和个人资料建立了许多模型连接,现在我很困惑。当我尝试删除用户时,出现错误。
IntegrityError at /admin/auth/user/
FOREIGN KEY constraint failed
我不太确定,但是我认为错误与以下模型中的following
有关。我尝试创建一个不关注任何人的新用户,并尝试将其删除,它确实有效!但是已经互相追随的那些不会被删除,并且会在上面出现错误
class Profile(models.Model):
# creating a one to oe rel with a user model
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(default='default.jpg', upload_to='profile_pics')
dob = models.DateField(blank=True, null=True)
bio = models.TextField(max_length=400, blank=True)
following = models.ManyToManyField('self', related_name='follows', symmetrical=False)
joined_at = models.DateField(auto_now_add=True)
这是我的帖子模型
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_posted = models.DateTimeField(default=timezone.now)
likes = models.ManyToManyField(User, blank=True, related_name='post_likes')
image = models.ImageField(null=False, blank=False, upload_to='post_images')