All I want is to get the body from my request, but I don’t understand how to do it, I tried request.data but it returns a string, after I tried using json.loads(request.data), but it just refused to work. I just want to parse the nested json, why is it so difficult in Django.
My code
class TestCheckView(views.APIView):
def post(self, request, pk):
result = Result()
test_id = request.data.get("id")
data = dict(request.data)
for question in data["questions"]:
print(type(question))
for answer in question["answers"]:
result.total += 1
if Answer.objects.get(id=answer["id"],question_id=question["id"]):
result.score += 1
result.test = Test.objects.get(id=test_id)
result.save()
what i want get
"id":1,
"title":"Тест по теме тестов",
"questions":[
{
"id":1,
"title":"Вопрос 1",
"answers":[
{
"id":1,
"title":"Ответ 1",
"is_right":true
},
{
"id":2,
"title":"Ответ 2"
}
]
},
{
"id":2,
"title":"Вопрос 2",
"answers":[
{
"id":3,
"title":"Ответ 1",
"is_right":true
}
]
}
]
}