This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Question | |
| fields = '__all__' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from rest_framework.decorators import api_view | |
| from rest_framework.response import Response | |
| from rest_framework import status | |
| from .models import Question | |
| from .serializers import QuestionSerializer | |
| @api_view(['GET', 'POST']) | |
| def questions_view(request): | |
| if request.method == 'GET': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['GET', 'POST']) | |
| def questions_view(request): | |
| if request.method == 'GET': | |
| questions = Question.objects.all() | |
| serializer = QuestionSerializer(questions, many=True) | |
| return Response(serializer.data) | |
| elif request.method == 'POST': | |
| serializer = QuestionSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| question = serializer.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['GET', 'PATCH', 'DELETE']) | |
| def question_detail_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| if request.method == 'GET': | |
| serializer = QuestionSerializer(question) | |
| return Response(serializer.data) | |
| elif request.method == 'PATCH': | |
| raise NotImplementedError("PATCH currently not supported") | |
| elif request.method == 'DELETE': | |
| raise NotImplementedError("DELETE currently not supported") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['GET', 'PATCH', 'DELETE']) | |
| def question_detail_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| if request.method == 'GET': | |
| serializer = QuestionSerializer(question) | |
| return Response(serializer.data) | |
| elif request.method == 'PATCH': | |
| serializer = QuestionSerializer(question, data=request.data, partial=True) | |
| if serializer.is_valid(): | |
| question = serializer.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChoiceSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Choice | |
| fields = ('choice_text',) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @api_view(['POST']) | |
| def choices_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| serializer = ChoiceSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| choice = serializer.save(question=question) | |
| return Response("Choice created with id %s" % (choice.id), status=status.HTTP_201_CREATED) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Question | |
| fields = ['id', 'pub_date', 'question_text', 'choice_set'] | |
| extra_kwargs = { | |
| 'choice_set': {'read_only': True} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| choice_set = serializers.SlugRelatedField(slug_field='choice_text', read_only=True, many=True) | |
| class Meta: | |
| model = Question | |
| fields = '__all__' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| choices = serializers.SlugRelatedField(slug_field='choice_text', read_only=True, many=True, source='choice_set') | |
| class Meta: | |
| model = Question | |
| fields = '__all__' |