'DRF --405 Method not allowed for update operation

enter image description hereI have done this before and may be it did work properly that time but now suddenly it's not working as it's expected to be.

The viewset.py:

   class ProfileViewSet(ModelViewSet):
      serializer_class = ProfileSerializer
      http_method_names = ["post", "get", "put", "delete"]
      queryset = Profile.objects.all()

      def get(self, request, format=None):
          users = Profile.objects.all()
          serializer = ProfileSerializer(users, many=True)
          return Response(serializer.data)

      def put(self, request, pk, format=None):
          snippet = self.get_object(pk)
          serializer = ProfileSerializer(snippet, data=request.data)
          if serializer.is_valid():
             serializer.save()
             return Response(serializer.data)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

models.py

class Profile(models.Model):
     user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    

     full_name = models.CharField(_("Name of User"), blank=True, null=True, 
     max_length=255)
    
     phone_number = PhoneNumberField()
     zip_code = models.CharField(_("Zip Code"), blank=True, null=True, max_length=255)
     def __str__(self):
         return self.user.username

seralizers.py

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__' 

urls.py

router.register("profile/<int:pk>", ProfileViewSet, basename="profile_each")

whenever I hit in postman It says "405 Method not allowed".

Am I missing anything here please?



Solution 1:[1]

If you are using a ViewClass, you need define it as a view.

router.register("profile/<int:pk>", ProfileViewSet.as_view(), basename="profile_each")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LordPokerFace