【发布时间】:2022-01-09 13:22:58
【问题描述】:
我正在通过 API 调用数据并希望将该数据写入我的 Django 模型。 API 为我提供了具有以下结构的 JSON:
"results": [
{
"key1": "value",
"key2": "value"
},
{
"key1": "value",
"key2": "value",
},
{
"key1": "value",
"key2": "value",
},
....
我的代码如下所示:
def get_data():
try:
response = requests.get(settings.HOST+settings.PATH, verify=False)
for data in response['results']:
print(data)
mydata = MyModel.add(**data)
return results
我收到此错误:
Connected to pydev debugger (build 212.5284.44)
{'key1': 'value', 'key2': 'value'}
list indices must be integers or slices, not str
Process finished with exit code 0
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 846, in exec_module
File "<frozen importlib._bootstrap_external>", line 983, in get_code
File "<frozen importlib._bootstrap_external>", line 913, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/user01/Repository/api-test/myapi/myapp/management/commands/getapidata.py", line 27
get_data()
IndentationError: unexpected unindent
python-BaseException
我做错了什么?
PyCharm 中的变量截图
【问题讨论】:
-
请提供完整的回溯。
-
请提供完整的堆栈跟踪,以便我们知道错误指的是什么。
-
包含这个严重裁剪的图像有什么意义?
-
@WillemVanOnsem 我添加了完整的回溯
-
是
response = requests.get(settings.HOST+settings.PATH, verify=False),不是response: requests.get(settings.HOST+settings.PATH, verify=False)。
标签: python json django list loops