[Django]-How to update a single field in a model using UpdateAPIView from Djangorestframework?

9๐Ÿ‘

โœ…

I figure the error is with your queryset value. if the target is a field in the
Contacts model, simply have the queryset as all items in the Contacts model. This way the lookup field can be a used to filter the entire dataset and edit the appropriate entry.

class UpdateMobileAPIView(generics.UpdateAPIView):
    queryset = Contacts.objects.all()
    serializer_class = ContactsSerializer
    lookup_field = 'pk'

    def update(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=True)

        if serializer.is_valid():
            serializer.save()
            return Response({"message": "mobile number updated successfully"})

        else:
            return Response({"message": "failed", "details": serializer.errors})
๐Ÿ‘คfgkinus

Leave a comment