# Path: voting_app/polls/models.py
# class Question(models.Model):
#     author = models.ForeignKey(User, related_name='questions')
#     question_text = models.CharField(max_length=300)
#     created_date = models.DateTimeField(auto_now_add=True)
#     updated_date = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ['-created_date']
# 
#     def __str__(self):
#         return self.question_text 
# 
# class Choice(models.Model):
#     question = models.ForeignKey(Question, related_name='choices', on_delete=models.CASCADE)
#     choice_text = models.CharField(max_length=200)
#     vote = models.PositiveIntegerField(db_index=True, default=0)
# 
#     def __str__(self):
#         return self.choice_text
# 
# class Vote(models.Model):
#     question = models.ForeignKey(Question, related_name='votes', on_delete=models.CASCADE)
#     vote_author = models.ForeignKey(User, related_name='votes', blank=True, null=True)
#     voter_ip = models.GenericIPAddressField(blank=True, null=True, db_index=True)
#     choice = models.ForeignKey(Choice, related_name='votes')
#     created_date = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         index_together = [
#             ["question", "vote_author"],
#             ["question", "voter_ip"],
#         ]


from rest_framework import serializers
from ..models import Question, Choice, Vote
        model = Choice
        fields = ['choice_id', 'choice_text', 'vote']
        read_only_fields = ['vote']


class QuestionSerializer(serializers.ModelSerializer):

    choices = ChoicesSerializerWithVote(many=True)

    class Meta:
        model = Question
        fields = ['id', 'question_text', 'choices']
        read_only_fields = ['id']

    def create(self, validated_data):
        choices = validated_data.pop('choices')
        question = Question.objects.create(**validated_data)
        for choice in choices:
            Choice.objects.create(question=question, **choice)
        return question

    def update(self, instance, validated_data):
        choices = validated_data['choices']
        instance.question_text = validated_data['question_text']
        instance.save()
        # Only create new choices, dont update existing ones
        for choice in choices:
            try:
                choice_id = choice['choice_id']
            except:

# Path: voting_app/polls/models.py
# class Question(models.Model):
#     author = models.ForeignKey(User, related_name='questions')
#     question_text = models.CharField(max_length=300)
#     created_date = models.DateTimeField(auto_now_add=True)
#     updated_date = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ['-created_date']
# 
#     def __str__(self):
#         return self.question_text 
# 
# class Choice(models.Model):
#     question = models.ForeignKey(Question, related_name='choices', on_delete=models.CASCADE)
#     choice_text = models.CharField(max_length=200)
#     vote = models.PositiveIntegerField(db_index=True, default=0)
# 
#     def __str__(self):
#         return self.choice_text
# 
# class Vote(models.Model):
#     question = models.ForeignKey(Question, related_name='votes', on_delete=models.CASCADE)
#     vote_author = models.ForeignKey(User, related_name='votes', blank=True, null=True)
#     voter_ip = models.GenericIPAddressField(blank=True, null=True, db_index=True)
#     choice = models.ForeignKey(Choice, related_name='votes')
#     created_date = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         index_together = [
#             ["question", "vote_author"],
#             ["question", "voter_ip"],
#         ]


# Path: voting_app/polls/api/serializers.py
from rest_framework import serializers
from ..models import Question, Choice, Vote

        model = Choice
        fields = ['choice_id', 'choice_text', 'vote']
        read_only_fields = ['vote']


class QuestionSerializer(serializers.ModelSerializer):

    choices = ChoicesSerializerWithVote(many=True)

    class Meta:
        model = Question
        fields = ['id', 'question_text', 'choices']
        read_only_fields = ['id']

    def create(self, validated_data):
        choices = validated_data.pop('choices')
        question = Question.objects.create(**validated_data)
        for choice in choices:
            Choice.objects.create(question=question, **choice)
        return question

    def update(self, instance, validated_data):
        choices = validated_data['choices']
        instance.question_text = validated_data['question_text']
        instance.save()
        # Only create new choices, dont update existing ones
        for choice in choices:
            try:
                choice_id = choice['choice_id']
            except:

                Choice.objects.create(