在Django Rest Framework(2.3.5)中序列化ManyToMany会抛出ValueError

时间:2013-07-29 23:00:39

标签: python django rest serialization django-rest-framework

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

  

ValueError:instance应该是一个queryset或其他可迭代的   许多=真

以下是观点:

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

2 个答案:

答案 0 :(得分:1)

问题似乎是嵌套表示do not support read–write,并且为写操作序列化平面结构是可行的方法。

答案 1 :(得分: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'
        )