Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save akshar-raaj/ca4c8956b52a906bf49afd0227f83848 to your computer and use it in GitHub Desktop.

Select an option

Save akshar-raaj/ca4c8956b52a906bf49afd0227f83848 to your computer and use it in GitHub Desktop.
Vote
@api_view(['PATCH'])
def vote_view(request, question_id):
question = get_object_or_404(Question, pk=question_id)
serializer = VoteSerializer(data=request.data)
if serializer.is_valid():
choice = get_object_or_404(Choice, pk=serializer.validated_data['choice_id'], question=question)
choice.votes += 1
choice.save()
return Response("Voted")
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment