0

Suppose, we have a ViewSet class:

class SomeViewSet(viewsets.ViewSet):
   def create(self, request):
      pass

   def custom_action(self, request):
      pass

and we register SomeViewSet as follows:

some_router = DefaultRouter()
some_router.register(r'some-route', SomeViewSet, basename='some-name')

So, now we have the SomeViewSet with the standard action create, which will be accessible with the route some-route/ using POST HTTP method.

The question is how to configure the custom_action action to be accessible by the same route as a standard create action (some-route/) with the PUT HTTP method.

Ahmed Hany
  • 952
  • 6
  • 12
Dmytro
  • 190
  • 8
  • If it's `PUT`, is it not an option to just implement what `custom_action` is doing in `update()`? – Brian Destura Oct 31 '21 at 22:11
  • @BrianDestura yes, but `update` is a `detail` action, so it will be required to pass a parameter to access a view. The route will be something like that `some-route/` instead of `some-route/` path. – Dmytro Nov 01 '21 at 10:48

1 Answers1

0

Try something like this:

class SomeViewSet(viewsets.ViewSet):
   def create(self, request):
      pass

   @action(detail=True, methods=["PUT"], url_name="custom_action")
   def custom_action(self, request):
      pass
Sardar Faisal
  • 594
  • 5
  • 20