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
| import datetime | |
| from django.db import models | |
| from django.utils import timezone | |
| class Question(models.Model): | |
| question_text = models.CharField(max_length=200) | |
| pub_date = models.DateTimeField('date published', null=True) | |
| author = models.CharField(max_length=200, null=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
| from rest_framework import filters | |
| class QuestionsAPIView(generics.ListCreateAPIView): | |
| search_fields = ['question_text'] | |
| filter_backends = (filters.SearchFilter,) | |
| queryset = Question.objects.all() | |
| serializer_class = QuestionSerializer |
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
| urlpatterns = [ | |
| path('questions/', views.QuestionsAPIView.as_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
| from rest_framework import generics | |
| from .models import Question | |
| from .serializers import QuestionSerializer | |
| class QuestionsAPIView(generics.ListCreateAPIView): | |
| queryset = Question.objects.all() | |
| serializer_class = QuestionSerializer |
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
| class Question(models.Model): | |
| question_text = models.CharField(max_length=200) | |
| pub_date = models.DateTimeField('date published', null=True) | |
| author = models.CharField(max_length=200, null=True) | |
| def __str__(self): | |
| return self.question_text | |
| class Choice(models.Model): | |
| question = models.ForeignKey(Question, on_delete=models.CASCADE) |
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
| if serializer.is_valid(): | |
| question = serializer.save() | |
| serializer = QuestionSerializer(question) | |
| return Response(serializer.data, status=status.HTTP_201_CREATED) # We removed the hardcoded "Question created with id %s". |
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
| elif request.method == 'DELETE': | |
| question.delete() | |
| return Response("Question deleted", status=status.HTTP_204_NO_CONTENT) |
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>/choices/', apiviews.choices_view, name='choices_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
| path('questions/<int:question_id>/', apiviews.question_detail_view, name='question_detail_view') |