• <legend id='cAI7m'><style id='cAI7m'><dir id='cAI7m'><q id='cAI7m'></q></dir></style></legend>

  • <tfoot id='cAI7m'></tfoot>
      <bdo id='cAI7m'></bdo><ul id='cAI7m'></ul>
    <i id='cAI7m'><tr id='cAI7m'><dt id='cAI7m'><q id='cAI7m'><span id='cAI7m'><b id='cAI7m'><form id='cAI7m'><ins id='cAI7m'></ins><ul id='cAI7m'></ul><sub id='cAI7m'></sub></form><legend id='cAI7m'></legend><bdo id='cAI7m'><pre id='cAI7m'><center id='cAI7m'></center></pre></bdo></b><th id='cAI7m'></th></span></q></dt></tr></i><div id='cAI7m'><tfoot id='cAI7m'></tfoot><dl id='cAI7m'><fieldset id='cAI7m'></fieldset></dl></div>

  • <small id='cAI7m'></small><noframes id='cAI7m'>

      1. Django 错误.不能分配必须是实例

        时间:2023-08-30
        <i id='0PI8d'><tr id='0PI8d'><dt id='0PI8d'><q id='0PI8d'><span id='0PI8d'><b id='0PI8d'><form id='0PI8d'><ins id='0PI8d'></ins><ul id='0PI8d'></ul><sub id='0PI8d'></sub></form><legend id='0PI8d'></legend><bdo id='0PI8d'><pre id='0PI8d'><center id='0PI8d'></center></pre></bdo></b><th id='0PI8d'></th></span></q></dt></tr></i><div id='0PI8d'><tfoot id='0PI8d'></tfoot><dl id='0PI8d'><fieldset id='0PI8d'></fieldset></dl></div>

            <bdo id='0PI8d'></bdo><ul id='0PI8d'></ul>
            <legend id='0PI8d'><style id='0PI8d'><dir id='0PI8d'><q id='0PI8d'></q></dir></style></legend>

              <small id='0PI8d'></small><noframes id='0PI8d'>

            • <tfoot id='0PI8d'></tfoot>

                    <tbody id='0PI8d'></tbody>

                  本文介绍了Django 错误.不能分配必须是实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试在我的一个表中运行插入时收到以下错误.

                  I get the following error when I try to run an insert into one of my tables.

                  不能分配1":Team.department_id"必须是Department"实例

                  Cannot assign "1": "Team.department_id" must be a "Department" instance

                  诚然,我有点不确定我是否正确使用了外键概念.下面给出了我试图运行的插入和来自我的 models.py 的片段.

                  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.

                  我想做的是当有人想要创建一个新团队时.他们必须将其附加到一个部门.因此,部门 ID 应该在两组表中.

                  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)
                  

                  推荐答案

                  不需要传递部门id,实例本身就足够了.以下应该可以正常工作:

                  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)
                  )
                  

                  请注意,永远不要将您的外来字段命名为 something_id.something 就足够了.从用户的角度来看,Django 旨在让事情变得简单,而 _id 后缀意味着您正在考虑数据库层.实际上,如果您将列命名为 department,django 会自动为您在数据库中创建 department_id 列.事情是这样的,你让 django 创建 department_id_id 这是相当愚蠢的.

                  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.

                  这篇关于Django 错误.不能分配必须是实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django Rest Framework - 在序列化程序中获取相关模型字段 下一篇:Django 管理员内联:select_related

                  相关文章

                      <bdo id='rYJrY'></bdo><ul id='rYJrY'></ul>

                    1. <i id='rYJrY'><tr id='rYJrY'><dt id='rYJrY'><q id='rYJrY'><span id='rYJrY'><b id='rYJrY'><form id='rYJrY'><ins id='rYJrY'></ins><ul id='rYJrY'></ul><sub id='rYJrY'></sub></form><legend id='rYJrY'></legend><bdo id='rYJrY'><pre id='rYJrY'><center id='rYJrY'></center></pre></bdo></b><th id='rYJrY'></th></span></q></dt></tr></i><div id='rYJrY'><tfoot id='rYJrY'></tfoot><dl id='rYJrY'><fieldset id='rYJrY'></fieldset></dl></div>

                      <small id='rYJrY'></small><noframes id='rYJrY'>

                    2. <legend id='rYJrY'><style id='rYJrY'><dir id='rYJrY'><q id='rYJrY'></q></dir></style></legend>
                      <tfoot id='rYJrY'></tfoot>