Partial Update Failed via Viewset Django Rest Framework

699 views
Skip to first unread message

Ayo Ayo

unread,
Sep 18, 2017, 5:11:49 AM9/18/17
to Django REST framework
I want to update a field in my model whenever an object link is called through the api. I'm using Django Rest Framework to handle the creation of api.

The link to a single object is

    example.com/api/pk/ where pk is the object id.

In the model viewset, I wrote a partial update method to handle the addition of 1 to the field whenever the object is called upon.

    class RocNationViewSet(viewsets.ModelViewSet):
        queryset=RocNation.objects.filter(is_active=True).order_by('-pub_date')
        serializer_class=RocNationSerializer
        filter_backends = (DjangoFilterBackend,)
   

        def partial_update(self, request, pk=None):
            serializer=RocNationSerializer(rocnation, roc_views=F('roc_views')+1, partial=True)
            serializer.save()
            return Response(serializer.data)

The method is not carrying out the operation. The field in the model is not updated. What am I missing?
Reply all
Reply to author
Forward
0 new messages