使用序列化程序在restframework中进行API调用的Django一对多关系

我有两个模型,它们是问题和与该问题一致的一组选择。我想结合这两个模型来创建一个 API,显示一个问题列表和他们的选择。


这些是我的模型:


class Question(models.Model):

    question_text = models.CharField(max_length=200)

    pub_date = models.DateTimeField("date published")


    def __str__(self):

        return self.question_text



class Choice(models.Model):

    choice_text = models.CharField(max_length=200)

    votes = models.IntegerField(default=0)

    question = models.ForeignKey(Question, on_delete=models.CASCADE)


    def __str__(self):

        return self.choice_text

这些是我的序列化程序


class QuestionSerializer(serializers.ModelSerializer):

    class Meta:

        model = Question

        fields = ('question_text',)



class ChoiceSerializer(serializers.ModelSerializer):

    class Meta:

        model = Choice

        fields = ('choice_text','question',)



class QuestionWithAnswer(serializers.ModelSerializer):

    question = QuestionSerializer(many=True)


    class Meta:

        model = Choice

        fields = ('question',)

这是视图类


@api_view(['GET', 'POST', ])

def getquestionWithChoices(request):

    question = Question.objects.all()

    serializer = QuestionWithAnswer

    return Response(serializer.data)

我尝试了很多东西,但无法获得以下输出。


  [

          {

            "id": 1,

            "question_text": "What's your name?",

            "pub_date": "2019-04-13T05:27:21Z",

            "choices": [

              {

                "id": 1,

                "choice_text": "fred",

                "votes": 0,

                "question": 1

              },

              {

                "id": 2,

                "choice_text": "bob",

                "votes": 0,

                "question": 1

              },

              {

                "id": 3,

                "choice_text": "rachel",

                "votes": 0,

                "question": 1

              }

            ]

          },

         


慕桂英4014372
浏览 148回答 2
2回答

慕桂英546537

您没有正确实例化序列化器。@api_view(['GET', 'POST', ])def getquestionWithChoices(request):    questions = Question.objects.all()    serializer = QuestionWithAnswer(questions, many=True)    return Response(serializer.data)并且您的序列化程序也未正确定义:class ChoiceSerializer(serializers.ModelSerializer):    class Meta:        model = Choice        fields = ('id', 'votes', 'choice_text','question',)class QuestionWithAnswer(serializers.ModelSerializer):    choices = ChoiceSerializer(many=True)    class Meta:        model = Question        fields = ('id', 'question_text', 'pub_date', 'choices')

catspeake

这是我的观点:@api_view(['GET', 'POST', ])def getquestionWithChoices(request):    questions = Question.objects.all()    serializer = QuestionWithAnswer(questions, many=True)    return Response(serializer.data)这是我的序列化器:class ChoiceSerializer(serializers.ModelSerializer):    class Meta:        model = Choice        fields = ('id', 'votes', 'choice_text',)class QuestionWithAnswer(serializers.ModelSerializer):    question = ChoiceSerializer(many=True)    class Meta:        model = Question        fields = ('id', 'question_text', 'pub_date','question')这是我的模型:class Choice(models.Model):    choice_text = models.CharField(max_length=200)    votes = models.IntegerField(default=0)    question = models.ForeignKey(Question,  related_name='question', on_delete=models.CASCADE)    def __str__(self):        return self.choice_text任何寻找相同的人都可以使用这个。所有剩余的代码都与问题相同。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python