【发布时间】:2019-02-01 13:29:20
【问题描述】:
我有一个大的序列化 json 字段,我试图从中提取多个键,然后构造另一个 json 并通过请求发送。
例子:
class ACtionViewSet(viewsets.ModelViewSet):
logger = logging.getLogger('django')
"""
API endpoint
"""
queryset = Action.objects.all()
serializer_class = ActionSerializer
filter_backends = (filters.OrderingFilter,
filters.SearchFilter, DjangoFilterBackend)
filterset_class = ActionFilter
# create new action for this workflow
def create(self, request, *args, **kwargs):
serializer = ActionSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
encode_data = json.dumps(serializer.data)
wanted_key = ['task', 'task_default', 'slug']
new_action = {x: encode_data[x] for x in wanted_key if x in encode_data}
print(new_action)
workflow = json.dumps(new_action)
response = requests.post(
url='{}/workflows'.format(MISTRAL_URL),
json=workflow,
headers=headers
)
# logging.debug(f"{self.response}")
return Response({'response': response}, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
错误是
string indices must be integers
追溯:
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/rest_framework/viewsets.py" in view
116. return self.dispatch(request, *args, **kwargs)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
495. response = self.handle_exception(exc)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/rest_framework/views.py" in handle_exception
455. self.raise_uncaught_exception(exc)
File "/home/copser/.local/share/virtualenvs/api-IztWJwuB/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
492. response = handler(request, *args, **kwargs)
File "/home/copser/Documents/Project/api/src/action/views.py" in create
76. new_workflow = {x: encode_data[x] for x in wanted_key if x in encode_data}
File "/home/copser/Documents/Project/NjiNN/njinn/api/src/action/views.py" in <dictcomp>
76. new_action = {x: encode_data[x] for x in wanted_key if x in encode_data}
Exception Type: TypeError at /api/v1/action
Exception Value: string indices must be integers
我有点困惑为什么会发生这种情况,所以有人可以解释一下这里发生了什么,谢谢
【问题讨论】:
-
你能发布完整的回溯吗?或者至少是引发错误的行号。
-
你的
ActionSerializer是什么样的? -
您已使用 json.dumps 将数据对象转换为字符串,然后在分配 new_action 时会引发此错误。 'some_string'['foo'] 会给你同样的错误。删除 json,dumps 可能会修复它,或者如果 request.data 是一个字符串,你应该改用 json.loads。
标签: python json django django-rest-framework