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
| # polls/models.py | |
| class Question(models.Model): | |
| .... | |
| def choices(self): | |
| if not hasattr(self, '_choices'): | |
| self._choices = self.choice_set.all() | |
| return self._choices | |
| # polls/serializers.py | |
| class ChoiceSerializer(serializers.ModelSerializer): |
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 = ChoiceSerializer(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 QuestionListPageSerializer(serializers.ModelSerializer): | |
| was_published_recently = serializers.BooleanField(read_only=True) | |
| class Meta: | |
| model = Question | |
| fields = '__all__' | |
| class QuestionDetailPageSerializer(QuestionListPageSerializer): |
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 = QuestionListPageSerializer(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
| class VoteSerializer(serializers.Serializer): | |
| choice_id = serializers.IntegerField() |
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
| path('questions/<int:question_id>/vote/', apiviews.vote_view, name='vote_view'), |
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(['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) |
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 ChoiceSerializerWithVotes(ChoiceSerializer): | |
| class Meta(ChoiceSerializer.Meta): | |
| fields = ChoiceSerializer.Meta.fields + ('votes',) | |
| class QuestionResultPageSerializer(QuestionListPageSerializer): | |
| choices = ChoiceSerializerWithVotes(many=True, 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
| @api_view(['GET']) | |
| def question_result_view(request, question_id): | |
| question = get_object_or_404(Question, pk=question_id) | |
| serializer = QuestionResultPageSerializer(question) | |
| return Response(serializer.data) |
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
| path('questions/<int:question_id>/result/', apiviews.question_result_view, name='question_result_view') |