Django Rest: Correct data isn't sent to serializer with M2M model
<p>I have a simple relational structure with projects containing several sequences with an intermediate meta model.</p>

<p>I can perform a GET request easily enough and it formats the data correctly. However, when I want to post the validated_data variable does not contain data formatted correctly, so I can't write a create/update method.</p>

<p>The data should look like:</p>

<pre><code>{'name': 'something', 
'seqrecords': [{
                'id': 5, 
                'sequence': 'ACGG...AAAA', 
                'name': 'Z78529',
                'order': 1
               },
               {
                'id': 6,
                'sequence': 'CGTA...ACCC',
                'name': 'Z78527',
                'order': 2
               }, 
}
</code></pre>

<p>But instead it looks like this:</p>

<pre><code>{'name': 'something',
 'projectmeta_set': [
                     OrderedDict([('order', 1)]),
                     OrderedDict([('order', 2)]),
                     OrderedDict([('order', 3)])
                    ]
}
</code></pre>

<p><strong>Serializers:</strong></p>

<pre><code>class ProjectMetaSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField(source='sequence.id')
    name = serializers.ReadOnlyField(source='sequence.name')

    class Meta:
        model = ProjectMeta
        fields = ['id', 'name', 'order']


class ProjectSerializer(serializers.HyperlinkedModelSerializer):
    seqrecords = ProjectMetaSerializer(source='projectmeta_set', many=True)

    class Meta:
        model = Project
        fields = ['id', 'name', 'seqrecords']
        ReadOnlyField = ['id']

    def create(self, validated_data):
        project = Project(name=validated_data['name'])
        project.save()
        # This is where it all fails
        for seq in validated_data['seqrecords']:
            sequence = SeqRecord.objects.filter(id=seq['id'])
            meta = ProjectMeta(project=project,
                               sequence=sequence,
                               order=seq['order'])
            meta.save()
        return project

class SeqRecordSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SeqRecord
        fields = ['id', 'name', 'sequence']
        read_only_fields = ['id']
</code></pre>

<p><strong>Models:</strong></p>

<pre><code>class SeqRecord(models.Model):
    name = models.CharField(max_length=50)
    sequence = models.TextField()


class Project(models.Model):
    name = models.CharField(max_length=50)
    sequences = models.ManyToManyField(SeqRecord,
                                       through='primer_suggestion.ProjectMeta')


class ProjectMeta(models.Model):
    project = models.ForeignKey(Project)
    sequence = models.ForeignKey(SeqRecord)
    order = models.PositiveIntegerField()
</code></pre>

<p><strong>View</strong></p>

<pre><code>class ProjectApiList(viewsets.ViewSetMixin, generics.ListCreateAPIView):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
</code></pre>

<p>Is there some way to change the validation of the data, so it returns the things I need or can I write the create and functions some other way?</p>

<p>As you see in the <code>ProjectMetaSerializer</code>, the fields <code>id</code> and <code>name</code> are ReadOnlyFields. So you can't use them in <code>post</code> request.</p>

<pre><code>class ProjectMetaSerializer(serializers.ModelSerializer):
    seqrecords = SeqRecordSerializer(many=True)

    class Meta:
        model = ProjectMeta
        fields = ['seqrecords',]


class ProjectSerializer(serializers.ModelSerializer):
    project_meta = ProjectMetaSerializer(many=True)

    class Meta:
        model = Project
        fields = ['id', 'name', 'project_meta']
        ReadOnlyField = ['id']

def create(self, validated_data):
    project = Project(name=validated_data['name'])
    project.save()
    # This is where it all fails
    for seq in validated_data['seqrecords']:
        sequence = SeqRecord.objects.filter(id=seq['id'])
        meta = ProjectMeta(project=project,
                           sequence=sequence,
                           order=seq['order'])
        meta.save()
    return project

class SeqRecordSerializer(serializers.ModelSerializer):
    class Meta:
        model = SeqRecord
        fields = ['id', 'name', 'sequence']
</code></pre>

python django django-rest-framework