Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing ManyToMany in Django Rest Framework (2.3.5) throws ValueError

I have what I assumed to be a pretty simple serializer setup, but when I attempt to PUT data to my API, I get the following error:

ValueError: instance should be a queryset or other iterable with many=True

Here's the view:

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

and the serializers:

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'
        )

If I comment out the 'questions' field from QuizSerializer it runs just fine (of course without question data).

** Edit: Here's the PUT call payload:

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
like image 869
Mortoc Avatar asked Jul 29 '13 23:07

Mortoc


1 Answers

The problem looks to be that nested representations do not support read–write, and serializing flat structures for write operations is the way to go.

like image 78
paulmelnikow Avatar answered Oct 11 '22 03:10

paulmelnikow