axios 将ID发送到DRF API,但调用名称

dxxyhpgq  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(180)

我真的很挣扎,因为我正在学习这一点。我试图调用以下:

{
        "id": 2,
        "interest_name": "Money"
    },
    {
        "id": 3,
        "interest_name": "Django"
    },
    {
        "id": 4,
        "interest_name": "Test"
    },
    {
        "id": 5,
        "interest_name": "Inventory Management"
    },
    {
        "id": 6,
        "interest_name": "Design"
    }

具有:

created () {
      getAPI.get('api/v1/projects/category')
      .then(response => {
        this.ProjectsAPI = response.data
        console.log('API Data Received')
      })
      .catch(errors => {
        console.log(errors)
      })

我显示了一个选择字段,如下所示:

<select id="interest_category" name="interest_category" v-model="interest_category" multiple class="block w-full max-w-lg rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:max-w-xs sm:text-sm">
   <option v-for="category in ProjectsAPI">{{ category.interest_name }}</option>
</select>

我在渲染的Vue页面中看到以下内容:

Money
Django
Test
Inventory Management
Design

现在,我尝试选择该选项,但当我点击提交时,我收到此错误:

[{"non_field_errors":["Invalid data. Expected a dictionary, but got str."]}]}

下面是我的表单代码:

submitForm(e) {
            const formData = {
                project_title: this.project_title,
                project_description: this.project_description,
                interest_category: this.interest_category,
                created_by: this.created_by
            }

            axios 
                .post("api/v1/projects/", formData)
                .then (response => {
                    console.log(response)

                    this.$router.push('/project-dashboard')
                })
                .catch(error => {
                    if (error.response) {
                        for (const property in error.response.data) {
                            this.errors.push(`${property}: ${error.response.data[property]}`)
                        }

                        console.log(JSON.stringify(error.response.data))
                    } else if (error.message) {
                        console.log(JSON.stringify(error.message))
                    } else {
                        console.log(JSON.stringify(error))
                    }
                })
        }
    }

我正在努力从第一个API调用中获取ID并传递它而不是项的名称。

[
    "Money",
    "Django",
    "Test",
    "Inventory Management",
    "Design",
]

如何将它们转换为从第一个API调用中获得的ID,以发送ID而不是interest_category的str

更新

我能够得到正确的值,所以现在它传递了正确的ID,如评论中所示。现在,我得到了这个错误。

Invalid data. Expected a dictionary, but got int.
6uxekuva

6uxekuva1#

假设Django在API中由于某种原因需要类别字典,那么submitForm方法应该如下所示:

submitForm(e) {
  const category = this.ProjectAPI.find(({ id }) => id == this.interest_category)
  const formData = {
    project_title: this.project_title,
    project_description: this.project_description,
    interest_category: category, // here
    created_by: this.created_by
  }

也请共享此端点的Django视图,以便我们可以修复您的问题
如果只是为了学习,你的代码片段看起来不错,但如果你开发一个生产应用程序,我建议你做一些修改,以提高代码的质量,使其更易于管理。

相关问题