Django Rest Create with a Writable Nested Serializer
<p>I'm trying to perform a create in Django Rest Framework using a writable nested serializer.</p>

<p>With the code bellow I can create a ScriptQuestion but I can't add a RecordedInterview into it. Django says OrderedDict is None.</p>

<p>What am I doing wrong?</p>

<p>Thanks in advance</p>

<pre><code>#models.py

class ScriptQuestion(models.Model):
    interview = models.ManyToManyField(RecordedInterview)
    ...

class RecordedInterview(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    ...
</code></pre>

<p>The serializers</p>

<pre><code>#serializers.py

class InterviewTitleSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = RecordedInterview
        fields = ('id', 'title')
        extra_kwargs = {
            'title': { 'read_only': True }
        }


class QuestionDetailSerializer(serializers.HyperlinkedModelSerializer):

    interview = InterviewTitleSerializer(many=True)

    class Meta:
        model = ScriptQuestion
        fields = ('id', 'title', 'prep_time', 'answer_time', 'interview')
        depth = 1

    def create(self, validated_data):
        interview_data = validated_data.pop('interview')
        question = ScriptQuestion.objects.create(**validated_data)
        for item in interview_data:
            item = interview_data['id']
            question.interview.add(item)
        return question
</code></pre>

<p>Here is my view</p>

<pre><code>#views.py 

class CreateQuestion(generics.CreateAPIView):
    queryset = ScriptQuestion.objects.all()
    serializer_class = QuestionDetailSerializer
</code></pre>

<p>And the json</p>

<pre><code>{
    "title": "Question Test Json",
    "prep_time": "1",
    "answer_time":"1",
    "interview": [
           {
                "id": "a450aeb0-8446-47b0-95bd-5accbb8b4afa"
            }
     ]
}
</code></pre>

<p>If I do manually, I can add the RecordedInterview into the ScriptQuestion:</p>

<pre><code>#serializers.py 

def create(self, validated_data):
    interview_data = validated_data.pop('interview')
    question = ScriptQuestion.objects.create(**validated_data)
    item = 'a450aeb0-8446-47b0-95bd-5accbb8b4afa'
    question.interview.add(item)
    return question
</code></pre>

<p>I cannot add a comment because of low reputation. SO adding as an answer. I think you should use 'serializers.ModelSerializer' instead of 'serializers.HyperLinkedModelSerializer'</p>

python django nested django-rest-framework