Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Choice(models.Model):
- question = models.ForeignKey(Poll, on_delete=models.CASCADE)
- choice_text = models.CharField(max_length=200)
- votes = models.IntegerField(default=0)
- def __str__(self):
- return self.choice_text
- class Poll(models.Model):
- question = models.CharField(max_length=200)
- pub_date = models.DateTimeField(auto_now=True)
- created_by = models.ForeignKey(User, on_delete=models.CASCADE)
- def __str__(self):
- """
- Models string represantation
- """
- return self.question
- ##Serializers
- class ChoiceSerializer(serializers.ModelSerializer):
- """Serialize all model fields for Choice model"""
- votes = VoteSerializer(many=True, read_only=True, required=False)
- class Meta:
- model = Choice
- fields = '__all__'
- ##Views
- class ChoiceCustomView(generics.ListCreateAPIView):
- """View to fetch choices given a certain poll"""
- def get_queryset(self):
- queryset = Choice.objects.filter(question_id=self.kwargs['pk'])
- return queryset
- serializer_class = ChoiceSerializer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement