Permalink
Browse files

Add serializer and view to show questions

  • Loading branch information...
andrewdotn committed Aug 20, 2016
1 parent 5705c54 commit bef198f2332ab6ef492652f96aa1c46bb3a4503a
Showing with 16 additions and 0 deletions.
  1. +7 −0 polls_rest/polls/serializers.py
  2. +2 −0 polls_rest/polls/urls.py
  3. +7 −0 polls_rest/polls/views.py
@@ -0,0 +1,7 @@
from rest_framework import serializers

from .models import Choice, Question

class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Question
@@ -8,4 +8,6 @@
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

url(r'^api/questions/$', views.QuestionViewSet.as_view({'get': 'list'}))
]
@@ -3,7 +3,10 @@
from django.shortcuts import get_object_or_404, render
from django.urls import reverse

from rest_framework import viewsets

from .models import Choice, Question
from .serializers import QuestionSerializer

def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
@@ -33,3 +36,7 @@ def vote(request, question_id):
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results',
args=(question.id,)))

class QuestionViewSet(viewsets.ModelViewSet):
queryset = Question.objects.all()
serializer_class = QuestionSerializer

0 comments on commit bef198f

Please sign in to comment.