Как я могу успешно POST выбор, который связан с вопросом в API опроса в Django?

Я создаю опрос / голосование api, я могу успешно POST вопрос, но я получаю следующую ошибку Choice() получил неожиданный аргумент ключевого слова 'вопрос', когда я пытаюсь создать выбор, который связан с вопросом, я не могу понять это.

Это модели

from django.db import models
import datetime
from django.utils import timezone
# Модели для опоросов
class Question(models.Model):
    poll_question = models.CharField(max_length=255, blank=False)
    title = models.CharField(max_length=255, blank=True)
    start_date = models.DateTimeField('date published', blank=False)
    
    def __str__(self):
        return self.poll_question
    
    def published_not_longage(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.start_date <= now
    
 
       
#Модель для Выбора
class Choice(models.Model):
    poll_question = models.ForeignKey(Question, on_delete=models.CASCADE)
    poll_question_choice = models.CharField(max_length=255)
    votes = models.IntegerField(default=0)




The choices serializer
class ChoiceSerializer(serializers.Serializer):
    poll_question_choice = serializers.CharField(max_length=255)
    
    def create(self, validated_data):
        return Choice.objects.create(**validated_data)


#The choices view

- The error is arising when I pass in the question instance in the serializer.save() method.

@api_view(['POST'])
def choice_details(request, pk):
    question = Question.objects.get(pk=pk)
    # question = get_object_or_404(Question, pk=pk)
    serializer = ChoiceSerializer(data=request.data)
    if serializer.is_valid():
        poll_question_choice = serializer.save(question=question)
        return Response(ChoiceSerializer(poll_question_choice).data, status=status.HTTP_201_CREATED)
    return  Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

В модели Choice поле названо poll_question, поэтому его нужно использовать (в отличие от question) в save:

poll_question_choice = serializer.save(poll_question=question)
Вернуться на верх