Reputation: 639
I get the following error when I try to run an insert into one of my tables.
Cannot assign "1": "Team.department_id" must be a "Department" instance
Admittedly I'm slightly unsure if I'm using the foreign key concept correctly. The insert I'm trying to run and a snippet from my models.py are given below.
What I'm trying to do is that when someone wants to create a new team. They have to attach it to a department. Therefore the department ID should 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)
Upvotes: 48
Views: 82516
Reputation: 323
In my case, it was a typo.
X.objects.filter(y__zfield=z_obj).
Here name of zfield was actually zField
with a capital F
Upvotes: 0
Reputation: 12080
You don't need to pass the department id, the instance itself is enough.
The following should work just fine:
new_team = Team(
nickname = team_name,
employee_id = employee_id,
department_id = Department.objects.get(password = password,
department_name = department_name))
Just a note: don't ever name your foreign fields something_id.
That something is enough. Django is meant to make things easy from the user's perspective and the _id
suffix means you're thinking of the database layer. In fact, if you named your column department
, django will automatically create department_id
column in the database for you.
The way things are, you're making Django create department_id_id
which is rather silly.
Upvotes: 75
Reputation: 1315
A different variant for @munsu 's answer. If you already have the id for the foreign field.
department_id = 2; #the department id for the new team
new_team = Team(
nickname = team_name,
employee_id = employee_id,
department_id = Department(id=department_id)
)
new_team.save()
# but
new_team.department_id.id # 2
new_team.department_id.department_name # None you cannot access any other fields
I don't know if this would be useful for any case, but here it is. :)
Upvotes: 0
Reputation: 2062
This came up first on a Google search so offering an alternative for newcomers. You can also do it this way in case you have handy access to the id and don't want to do another query:
new_team = Team(
nickname = team_name,
employee_id = employee_id,
department_id_id = Department.objects.get(password = password, department_name = department_name).department_id
)
In short, {foreign_key_name}_id
if you want to assign the id directly.
Upvotes: 13