10

我有我认为是一个非常简单的序列化程序设置,但是当我尝试将数据放入我的 API 时,我收到以下错误:

ValueError:实例应该是一个查询集或其他可迭代的 many=True

这是视图:

class QuizAPI(generics.RetrieveUpdateDestroyAPIView):
    model = Quiz
    serializer_class = QuizSerializer
    lookup_field = 'activity_id'

和序列化器:

class MediaResourceSerializer(serializers.ModelSerializer):
    owner = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = MediaResource
        fields = ('owner', 'name', 'type', 'url')

class AnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Answer
        fields = ('text','is_correct','order')

class QuestionSerializer(serializers.ModelSerializer):
    answers = AnswerSerializer(many=True, required=False)
    resources = MediaResourceSerializer(many=True, required=False)

    class Meta:
        model = Question
        fields = ( 
            'question_id', 
            'type', 
            'order', 
            'question_text', 
            'explanation', 
            'resources', 
            'grading_weight', 
            'answers' 
        )

class QuizSerializer(serializers.ModelSerializer):
    questions = QuestionSerializer(many=True, required=False)
    created_by = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = Quiz
        fields = (
            'activity_id', 
            'name',  
            'is_hidden', 
            'created_by', 
            'created_date', 
            'last_updated', 
            'sharable', 
            'questions'
        )

如果我从 QuizSerializer 中注释掉“问题”字段,它运行得很好(当然没有问题数据)。

** 编辑:这是 PUT 调用有效负载:

activity_id: 4
created_by: 1
created_date: "2013-07-29T20:39:47.981Z"
is_hidden: false
last_updated: null
name: "This is only a test"
questions: []
sharable: true
4

2 回答 2

1

问题看起来是嵌套表示不支持读写,并且为写操作序列化平面结构是要走的路。

于 2014-01-20T01:50:36.593 回答
0

在我看来,对于模型之间的嵌套关系,最好和最简单的方法是使用不同的序列化器

我的情况

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()

    def get_serializer_class(self):
        if self.request.method == 'GET':
            return GetUserModelSerializer
        else:
            return UserModelSerializer

你的情况:

class QuizAPI(generics.RetrieveUpdateDestroyAPIView):
    model = Quiz
    #serializer_class = QuizSerializer
    lookup_field = 'activity_id'

    def get_serializer_class(self):
        if self.request.method == 'GET':
            return QuizNestedSerializer
        else: # Means POST, PUT etc.
            return QuizSimpleSerializer

class QuizSimpleSerializer(serializers.ModelSerializer):
    # remove these lines
    # questions = QuestionSerializer(many=True, required=False)
    # created_by = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = Quiz
        fields = (
            'activity_id', 
            'name',  
            'is_hidden', 
            'created_by', 
            'created_date', 
            'last_updated', 
            'sharable', 
            'questions'
        )

class QuizNestedSerializer(serializers.ModelSerializer):
    questions = QuestionSerializer(many=True, required=False)
    created_by = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = Quiz
        fields = (
            'activity_id', 
            'name',  
            'is_hidden', 
            'created_by', 
            'created_date', 
            'last_updated', 
            'sharable', 
            'questions'
        )
于 2018-11-16T15:24:05.670 回答