无法向django中的用户模型添加两个外键
Unable to add two foreign keys to the user model in django
我有一个用户模型,我已经扩展它来存储额外的信息。现在我有一个部门 table,它有两个键(外键),它们将与用户 table 中的列相关。但是我收到一个错误。
这是我试过的
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
STATUS_CHOICES = (
(1, ("Permanent")),
(2, ("Temporary")),
)
GENDER_CHOICES = (
(1, ("Male")),
(2, ("Female")),
(3, ("Not Specified"))
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
emp_type = models.IntegerField(choices=STATUS_CHOICES, default=1)
contact = models.CharField(max_length=13, blank=True)
whatsapp = models.CharField(max_length=13, blank=True)
gender = models.IntegerField(choices=GENDER_CHOICES, default=3)
avatar = models.ImageField(upload_to='users/images', default='users/images/default.jpg')
manager_username = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
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()
class Department(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=20, unique=True)
manager = models.ForeignKey(User, blank=True, null=True, related_name='manager_usernames', on_delete=models.DO_NOTHING)
tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
错误信息
错误:
users.Department.tech_lead: (fields.E304) Reverse accessor for 'Department.tech_lead' clashes with reverse accessor for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Department.tech_lead: (fields.E305) Reverse query name for 'Department.tech_lead' clashes with reverse query name for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Profile.manager_username: (fields.E304) Reverse accessor for 'Profile.manager_username' clashes with reverse accessor for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
users.Profile.manager_username: (fields.E305) Reverse query name for 'Profile.manager_username' clashes with reverse query name for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
可是我在tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
这一行的时候。一切正常。谁能告诉我我哪里出错了。只有当我在我的数据库中添加部门模型时才会出现这个问题,否则用户模型工作得很好。
编辑: 正如评论中指出的那样,我对 to_field
和 related_name
感到困惑。我在模型中混淆了 related_name 和 to_field。
如错误所述,Profile.manager_username
的 related_name 与 Department.tech_lead
的冲突。改变其中一个,使它们不一样。
(虽然我必须说,你的建模很奇怪。一个技术主管肯定不能负责多个部门吗?所以这种关系应该是一对一的。为什么明确link一个经理的个人资料,而不是一个部门?)
我有一个用户模型,我已经扩展它来存储额外的信息。现在我有一个部门 table,它有两个键(外键),它们将与用户 table 中的列相关。但是我收到一个错误。
这是我试过的
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
STATUS_CHOICES = (
(1, ("Permanent")),
(2, ("Temporary")),
)
GENDER_CHOICES = (
(1, ("Male")),
(2, ("Female")),
(3, ("Not Specified"))
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
emp_type = models.IntegerField(choices=STATUS_CHOICES, default=1)
contact = models.CharField(max_length=13, blank=True)
whatsapp = models.CharField(max_length=13, blank=True)
gender = models.IntegerField(choices=GENDER_CHOICES, default=3)
avatar = models.ImageField(upload_to='users/images', default='users/images/default.jpg')
manager_username = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
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()
class Department(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=20, unique=True)
manager = models.ForeignKey(User, blank=True, null=True, related_name='manager_usernames', on_delete=models.DO_NOTHING)
tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
错误信息 错误:
users.Department.tech_lead: (fields.E304) Reverse accessor for 'Department.tech_lead' clashes with reverse accessor for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Department.tech_lead: (fields.E305) Reverse query name for 'Department.tech_lead' clashes with reverse query name for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Profile.manager_username: (fields.E304) Reverse accessor for 'Profile.manager_username' clashes with reverse accessor for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
users.Profile.manager_username: (fields.E305) Reverse query name for 'Profile.manager_username' clashes with reverse query name for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
可是我在tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
这一行的时候。一切正常。谁能告诉我我哪里出错了。只有当我在我的数据库中添加部门模型时才会出现这个问题,否则用户模型工作得很好。
编辑: 正如评论中指出的那样,我对 to_field
和 related_name
感到困惑。我在模型中混淆了 related_name 和 to_field。
如错误所述,Profile.manager_username
的 related_name 与 Department.tech_lead
的冲突。改变其中一个,使它们不一样。
(虽然我必须说,你的建模很奇怪。一个技术主管肯定不能负责多个部门吗?所以这种关系应该是一对一的。为什么明确link一个经理的个人资料,而不是一个部门?)