Я знаю, что есть подобные вопросы, но я прошел через все и не нашел правильного ответа.
У меня есть Question
модель и POST
метод в моем APIView
выглядит следующим образом:
def post(self, request):
"""Create a new question"""
description = request.data['description']
question_type = request.data['type']
answers_to_close = request.data['to_close']
question = Question.objects.create(
description=description,
question_type=question_type,
answers_to_close=answers_to_close
)
return Response({
'status': 'SUCCESS', 'question_id': question.id,
'message': 'New question has been created successfully!',
'question': f"'{description}'"
})
Работает нормально, но я также хочу добавить возможность создавать несколько вопросов в рамках одного запроса. Я пытался что-то вроде этого (другой экземпляр APIView
:
def post(self, request, **kwargs):
"""Create a session of questions"""
session = kwargs['session']
description = request.data['description']
question_type = request.data['type']
answers_to_close = request.data['to_close']
Question.objects.create(
description=description,
question_type=question_type,
answers_to_close=answers_to_close,
session=session
)
return Response({
'status': 'SUCCESS', 'session': session,
'message': 'A new session has been created!'
})
Так что, когда я попробую этот запрос curl:
curl -X POST http://127.0.0.1:8000/api/sessions/create/21383/ --data
['description=Am I Strong&type=YESNO&to_close=3', 'description=Best js
framework&type=TEXT&to_close=10']
Там написано django.utils.datastructures.MultiValueDictKeyError: 'description' '
Что со мной не так APIView
?