我有一个 React/Django 应用程序,用户可以在其中回答多项选择问题。我按照这个确切的顺序将“choices”数组渲染到 UI 上。
{
"id": 2,
"question_text": "Is Lebron James the GOAT?",
"choices": [
{
"id": 5,
"choice_text": "No",
"votes": 0,
"percent": 0
},
{
"id": 4,
"choice_text": "Yes",
"votes": 1,
"percent": 100
}
],
}
当我在开发模式下选择一个选项时,我会向 Django 发送一个请求,以增加该选项的投票计数器,并且它将以相同的顺序发回带有更新投票的响应。当我尝试使用 npm run build 在生产模式中选择一个选项时,顺序会被切换。
{
"id": 2,
"question_text": "Is Lebron James the GOAT?",
"choices": [
{
"id": 4,
"choice_text": "Yes",
"votes": 1,
"percent": 50
},
{
"id": 5,
"choice_text": "No",
"votes": 1,
"percent": 50
}
]
}
我认为 JSON 数组的顺序必须保留。谁能解释为什么会发生这种情况?我几乎可以肯定这个问题源自 Django。这是 Django 上的函数视图。
@api_view(['POST'])
def vote_poll(request, poll_id):
if request.method == 'POST':
poll = Poll.objects.get(pk=poll_id)
selected_choice = Choice.objects.get(pk=request.data['selected_choice_id'])
selected_choice.votes += 1
selected_choice.save()
poll_serializer = PollAndChoicesSerializer(poll)
return Response({ 'poll': poll_serializer.data })
汪汪一只猫
相关分类