У меня есть модель User и модель позиции, где Position и модель User имеют имя пользователя и имя позиции в качестве уникального ключа. Я хочу создать отдельную таблицу (которая будет таблицей отношений), где я могу связать каждого пользователя с его позицией. Это то, что я пробовал:
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)
Это работало нормально, пока я не перенес таблицу позиций, но когда я добавляю таблицу отношений, т.е. 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)
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Himanshu Poddar\Desktop\ATG IInternship\INTRANET\Intranet\users\models.py", line 53, in <module>
class Emp_position:
File "C:\Users\Himanshu Poddar\Desktop\ATG IInternship\INTRANET\Intranet\users\models.py", line 56, in Emp_position
position_name = models.ForeignKey(Position, related_name='position', to_field='name', on_delete=models.CASCADE)
File "C:\Users\Himanshu Poddar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\related.py", line 792, in __init__
RECURSIVE_RELATIONSHIP_CONSTANT,
AssertionError: ForeignKey(<class 'users.models.Position'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
Почему я получаю эту ошибку и как я могу ее исправить, любая помощь будет высоко оценена.