I get the following error when I try to start pasting into one of my tables.
Cannot assign "1": "Team.department_id" must be an instance of "Department"
In truth, I'm not sure if I am using the foreign key concept correctly. The insert I am trying to run and the snippet from my .py models are given below.
What I'm trying to do is that when someone wants to create a new team. They must attach it to the department. Therefore, the department identifier must be in both sets of tables.
new_team = Team(
nickname = team_name,
employee_id = employee_id,
department_id = int(Department.objects.get(password = password, department_name = department_name).department_id)
)
models.py
class Department(models.Model):
department_id = models.AutoField(auto_created=True, primary_key=True, default=1)
department_name = models.CharField(max_length=60)
head_id = models.CharField(max_length=30)
password = models.CharField(max_length=128)
class Team(models.Model):
team_id = models.AutoField(primary_key=True)
department_id = models.ForeignKey('Department', related_name = 'Department_id')
employee_id = models.CharField(max_length=30)
nickname = models.CharField(max_length=60)
team_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
enter the code here
source
share