我有一个用户模型和一个职位模型,其中职位和用户模型都有用户名和职位名称作为唯一键。我想创建一个单独的表(这将是一个关系表),我可以将每个用户与他们的位置相关联。
这是我尝试过的:
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, to_field='username',related_name='manager_username', on_delete=models.DO_NOTHING)
def __str__(self):
return self.user.username
class Position:
name = models.CharField(max_length=20, unique=True)
class Emp_position:
emp_uname = models.ForeignKey(User, related_name='emp_name', to_field='username', on_delete=models.CASCADE)
position_name = models.ForeignKey(Position, related_name='position', to_field='name', on_delete=models.CASCADE)
在我迁移 Position 表之前它工作正常,但是当我添加关系表即 Emp_position 时,我收到一个错误:
Traceback (most recent call last):
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\related.py", line 786, in __init__
to._meta.model_name
AttributeError: type object 'Position' has no attribute '_meta'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
为什么我会收到此错误,我该如何解决?
白猪掌柜的
相关分类