1

I have a viewset that I created an @action decorated function.

class StoreOffersViewSet(viewsets.ViewSet):
    """Viewset for yoga stuff."""

    @action(detail=False, methods=['put'], name='yoga update')
        def update_yoga(self, request):
            # Get Kwargs passed
            params = self.kwargs

In my urls, I have:

router = DefaultRouter(trailing_slash=True)
router.register(r'yoga', views.StoreOffersViewSet, basename='yoga')

urlpatterns = router.urls

I would like that when a user has a PUT request to site.com/yoga, my update_yoga function is called.

As it stands now, I get the following error:

{
    "detail": "Method \"PUT\" not allowed."
}

My guess is that this is because for the default router, GET and POST already have the url style from the docs, https://www.django-rest-framework.org/api-guide/routers/#defaultrouter. So I am not too sure how to proceed. I am on django 3.8>

user875139
  • 1,599
  • 4
  • 23
  • 44

1 Answers1

-1

I think the problem you are facing may be in the URL you are calling. For performing the action you mentioned above you will need to use this URL, "/yoga/<int:pk>/update_yoga"

If you want that your put request should call update_yoga, then you can override the default put function.

for example,

class YogaViewSet(viewsets.ModelViewSet):
    querset = Yoga.objects.all()
    serializer_class = YogaSerializers
    
    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)