Updating an ManyToMany field with Django rest
<p>I'm trying to set up this API so I can use a "PUT" to  update one/many "TAG"s on an item in the model "MOVIE". Tags is an M2M on MOVIE. I am posting on the PK of the item in movie.</p>

<p>My httpie work (returns a 200OK) but nothing gets created. When I post the whole JSON (using fetch) it just creates the TAG but no M2M relationship on MOVIE (<a href="http://dpaste.com/0VCP53P" rel="noreferrer">link</a>).</p>

<p>httpie</p>

<pre><code>http -f PUT http://localhost:8000/api/Edit/3/ tag:='{"name": "TEST"}'
</code></pre>

<p>Models.py</p>

<pre><code>class Tag(models.Model):
    name = models.CharField("Name", max_length=5000, blank=True)
    taglevel = models.IntegerField("Tag level", null=True, blank=True)

class Movie(models.Model):
    title = models.CharField("Whats happening?", max_length=10000, blank=True)
    tag = models.ManyToManyField('Tag', blank=True)
</code></pre>

<p>Serializers.py</p>

<pre><code>class Tag1Serializer(serializers.ModelSerializer):
    class Meta:
        model = Tag
        fields = ('name',)

class EditSerializer(serializers.ModelSerializer):
    tag = Tag1Serializer(many=True, read_only=True)
    class Meta:
            model = Movie
            fields = ('title', 'tag', 'info', 'created',  'status')

    def update(self, instance, validated_data):
        import pdb; pdb.set_trace()
        tags_data = validated_data.pop('tag')
        for tag_data in tags_data:
            tag_qs = Tag.objects.filter(name__iexact=tag_data['name'])
            if tag_qs.exists():
                tag = tag_qs.first()
            else:
                tag = Tag.objects.get(**tag_data)
            instance.tag.add(tag)
        return movie
</code></pre>

<p>Views.py</p>

<pre><code>class MovieViewSet(viewsets.ModelViewSet):
    queryset = Movie.objects.all()
    serializer_class = MovieSerializer
</code></pre>

<p>Error:</p>

<pre><code>Traceback
    tags_data = validated_data.pop('tag')
KeyError: 'tag'
</code></pre>