django-rest-framework - DRF 在使用 Axios 发布的 JSON 对象中获取空数组
问题描述
我正在尝试将数据从 Axios 发布到 Django Rest Framework API 端点。
我的数据包含嵌套对象,如下所示:
{
'name': 'foo',
'index': 1,
'nested': [
{
'some_key': 'some_value,
'geojson_data': { ... }
},
]
}
编辑:nested
属性包含内部带有geojson数据的对象数组(由解析.geojson文件产生),所以它很深。
我的 axios 函数在这里定义:
async post(data) {
return axios
.post(API_URL + 'endpoint/',
data,
{
headers: authHeader()
})
.then(response => {
return response.data;
})
}
authHeader()
用于提供 Bearer 令牌授权。
我检查传递给 axios 的数据是否正确。在我的 DRF 视图中,我处理发布请求以处理嵌套输入数据(一对多关系)。
def post(self, request, format=None):
print(request.data) #empty nested array
# Do something with my data
如果我 print request.data
,nested
数组是空的,但其他属性不是。看起来像 :
{
'name': 'foo',
'index': 1,
'nested': []
}
我尝试使用 pytest 测试我的 API,并且我的测试通过了类似的数据。那么为什么嵌套对象没有被处理呢?这是我的 axios 调用的问题吗?
我已经读过 axios 可以发送 Json 对象而不需要对其进行字符串化,并且 DRF 也可以从中获取 JSON 数据,request.data
那么我错在哪里?
我还尝试添加application/json
到 headers 的 content-typ 属性,但没有成功。
解决方案
推荐阅读
- javascript - 无法读取 reactjs 中未定义的属性“名称”
- python-3.x - Discord.py 重写 Modmail 系统
- testing - Cypress- if 语句 - 如果元素在特定页面上不可用,则跳过部分测试代码
- git - Atom(适用于 Mac)和 Bitbucket
- java - 线程池/期货的正确做法?
- android - 生物识别提示宽度问题三星 S8
- arrays - 在没有使用网格网格的 for 循环的情况下在数组中心创建球体会创建外壳工件
- javascript - AWS 使用 Node.js 开发工具包调用本地 Lambda 端点
- json - 如何在 Oracle 中返回 JSON 更新文档?
- r - 使用 dplyr 将函数应用于每个组并返回指定的数据帧