Permalink
Browse files

Add increment verb

  • Loading branch information...
andrewdotn committed Aug 21, 2016
1 parent 5d9b191 commit 89ce5e01fb8f74ffbcd53c5fce467e0c667af416
Showing with 10 additions and 0 deletions.
  1. +10 −0 polls_rest/polls/views.py
@@ -2,7 +2,9 @@
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from .models import Choice, Question
from .serializers import ChoiceSerializer, QuestionSerializer
@@ -44,3 +46,11 @@ class QuestionViewSet(viewsets.ModelViewSet):
class ChoiceViewSet(viewsets.ModelViewSet):
queryset = Choice.objects.all()
serializer_class = ChoiceSerializer
# http://www.django-rest-framework.org/api-guide/routers/#extra-link-and-actions
@detail_route(['POST'])
def increment(self, request, pk):
choice = get_object_or_404(Choice, pk=pk)
choice.votes = F('votes') + 1
choice.save()
return self.retrieve(request, pk=pk)

0 comments on commit 89ce5e0

Please sign in to comment.