У меня есть большое сериализованное поле 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
Traceback:
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
Я немного запутался, почему это происходит, поэтому кто-то может объяснить, пожалуйста, что здесь происходитспасибо