我收到以下错误: dsapi.Profile.user: (fields.E304) Reverse accessor for 'Profile.user' clashes with reverse accessor for 'Profile.user'. HINT: Add or change a related_name argument to the definition for 'Profile.user' or 'Profile.user'.dsapi.Profile.user: (fields.E305) Reverse query name for 'Profile.user' clashes with reverse query name for 'Profile.user'.我的所有研究都表明可以related_name为每个问题创建一个。但就我而言,它告诉我反向访问器本身有问题。这里有人有什么想法吗?下面是我的 models.py 文件中有问题的代码。import uuidfrom django.db import modelsfrom django.contrib.auth.models import Userfrom django.db.models.signals import post_savefrom django.dispatch import receiverclass DsapiModel(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) created_at = models.DateTimeField(auto_now_add=True, editable=False) updated_at = models.DateTimeField(auto_now=True, editable=False) class Meta: abstract = Trueclass Profile(DsapiModel): """ Extend User model using one-to-one link Save a query using users = User.objects.all().select_related('profile') """ user = models.OneToOneField(User, on_delete=models.CASCADE) favorite_sites = models.ManyToManyField(Site) def __str__(self): return self.user.username@receiver(post_save, sender=User)def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance)@receiver(post_save, sender=User)def save_user_profile(sender, instance, **kwargs): instance.profile.save()
1 回答
皈依舞
TA贡献1851条经验 获得超3个赞
该错误表明Profile <-> User
您的项目中存在多个关系。
任意,它们之间可以有尽可能多的关系,但是,它们related_name
必须是不同的。
这是一篇 SO 帖子,可以帮助您理解related_name
添加回答
举报
0/150
提交
取消