How to update a HyperlinkedRelatedField in Django Rest Framework
<p>I'm trying to update a HyperlinkedRelatedField connected to a ManyToManyField in Django through Django Rest Framework and coming up with a successful PUT (status 200) that ignores my HyperlinkedRelatedField data. I'm obviously missing something, but what?</p>

<pre><code>#models.py

class Product(models.Model):

    name = models.CharField(_('Name'),)
    number = models.IntegerField(_('Number'),)
    slug = models.SlugField(_('Slug'),)


class UserPrefs(models.Model):

    other_prefs = models.CharField(_('Other Prefs'))
    favorite_products = models.ManyToManyField(
        Product,
        blank = True)


#views.py

class UserPrefsViewSet(viewsets.ModelViewSet):

    permission_classes = (
        permissions.IsAuthenticated,
    )
    serializer_class = UserPrefsSerializer

    def get_queryset(self):
        user = self.request.user
        return UserPrefs.objects.filter(user=user)


#serializers.py    

class ProductSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Product
        fields = (
            'url',
            'name',
            'number',
            'slug',
            )

        extra_kwargs = {
            'url': {'lookup_field': 'slug'}
        }


class UserPrefsSerializer(serializers.HyperlinkedModelSerializer):

    favorite_products = serializers.HyperlinkedRelatedField(
        queryset = Product.objects.all(),
        many = True,
        read_only = False,
        view_name = 'product-detail',
        lookup_field = 'slug',)

    class Meta:
        model = UserPrefs
        fields = (
            'url',
            'other_prefs',
            'favorite_products',
        )
</code></pre>

<p>For example, using httpie from the command line, my PUT:</p>

<pre><code>http -a my_auth_details PUT http://localhost/api_v2/userprefs/1/ {favorite_products: ['http://localhost/api_v2/product/jacket/', 'http://localhost/api_v2/product/shirt/']}
</code></pre>

<p>...and the result:</p>

<pre><code>HTTP/1.0 200 OK

{
    "favorite_products": [],  
    "url": "http://localhost:8000/api_v2/userprefs/1/"
}
</code></pre>

<p>it seems like you didn't override the <strong>create()</strong> method of <strong>UserPrefsSerializer().</strong></p>

<pre><code>class UserPrefsSerializer(serializers.HyperlinkedModelSerializer):

    favorite_products = serializers.HyperlinkedRelatedField(
        queryset = Product.objects.all(),
        many = True,
        read_only = False,
        view_name = 'product-detail',
        lookup_field = 'slug',)

    class Meta:
        model = UserPrefs
        fields = (
            'url',
            'other_prefs',
            'favorite_products',
        )

    def create(self, validated_data):

        favorite_products_data = validated_data.pop('favorite_products')

        user_prefs = UserPrefs.objects.create(**validated_data)

        for favorite_product_data in favorite_products_data:
            favorite_product = Product.objects.create(**favorite_product_data)
            user_prefs.favorite_products.add(favorite_product)

        return user_prefs

    def update(self, instance, validated_data):

        favorite_products_data = validated_data.pop('favorite_products')

        instance.other_prefs = validated_data.get('other_prefs', instance.other_prefs)

        instance.save()

        for favorite_product_data in favorite_products_data:
            favorite_product = Product(**favorite_product)
            favorite_product.save()

        return instance
</code></pre>

<p>Please check the <a href="http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers" rel="nofollow">Writable nested serializers</a> </p>

python json django django-rest-framework