Django error. Cannot assign must be an instance

后端 未结 1 1204
死守一世寂寞
死守一世寂寞 2020-12-14 07:30

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\" inst

相关标签:
1条回答
  • 2020-12-14 08:05

    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.

    0 讨论(0)
提交回复
热议问题