我有我认为是一个非常简单的序列化程序设置,但是当我尝试将数据放入我的 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