Django rest framework 我有嵌套的序列化程序';,并希望通过重写create方法为其创建实例。

Django rest framework 我有嵌套的序列化程序';,并希望通过重写create方法为其创建实例。,django-rest-framework,Django Rest Framework,我的测验应用程序有两种型号,如下所示:- class Answer_Options(models.Model): text = models.CharField(max_length=200) 及 现在,我想为上述模型制作序列化程序,如下所示:- class Answer_OptionsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Answer_Options

我的测验应用程序有两种型号,如下所示:-

class Answer_Options(models.Model):
    text = models.CharField(max_length=200)

现在,我想为上述模型制作序列化程序,如下所示:-

class Answer_OptionsSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Answer_Options
        fields = ('url', 'text')
对于这样的问答题:-

class Quiz_QuestionSerializer(serializers.HyperlinkedModelSerializer):
    possible_answers = Answer_OptionsSerializer(many=True)
    correct = Answer_OptionsSerializer()
    class Meta:
        model = Quiz_Question
        fields = ('url', 'quiz', 'q_type', 'text', 'possible_answers', 'selected', 'correct')
def create(self, validated_data):
        possible_answers_data = validated_data.pop('possible_answers')
        selected_answers_data = validated_data.pop('selected')
        correct_answers_data = validated_data.pop('correct')
        quiz_question = Quiz_Question.objects.create(**validated_data)
        if possible_answers_data:
            for answer in possible_answers_data:
                answer, created  = Answer_Options.objects.get_or_create(text=answer['text'])     
                if (answer.text == correct_answers_data['text']):
                    print answer.text
                    quiz_question.correct = answer                               
                quiz_question.possible_answers.add(answer)
        return quiz_question

现在我面临的问题是如何重写
create()
方法,这样我就可以保存该问题的
可能的
答案,还可以保存
选定的
以及
更正
选项,其中
选定的
更正
选项必须是
可能的
答案列表中的值。例如,如果我的
可能的\u答案列表是['A','B','C','D'],则所选
的值以及
正确的
字段必须是
'A','B','C'或'D'
。提前谢谢。

我想出了这样一种方法:-

class Quiz_QuestionSerializer(serializers.HyperlinkedModelSerializer):
    possible_answers = Answer_OptionsSerializer(many=True)
    correct = Answer_OptionsSerializer()
    class Meta:
        model = Quiz_Question
        fields = ('url', 'quiz', 'q_type', 'text', 'possible_answers', 'selected', 'correct')
def create(self, validated_data):
        possible_answers_data = validated_data.pop('possible_answers')
        selected_answers_data = validated_data.pop('selected')
        correct_answers_data = validated_data.pop('correct')
        quiz_question = Quiz_Question.objects.create(**validated_data)
        if possible_answers_data:
            for answer in possible_answers_data:
                answer, created  = Answer_Options.objects.get_or_create(text=answer['text'])     
                if (answer.text == correct_answers_data['text']):
                    print answer.text
                    quiz_question.correct = answer                               
                quiz_question.possible_answers.add(answer)
        return quiz_question

但是我的quick_question.correct=答案没有保存,我也没有收到任何错误或异常。