0

I am trying to use Django Rest Framework to perform an additional action from a viewset. In the urls I have:

router = SimpleRouter()
router.register(r'mymodels', MyModelViewSet, basename='mymodel')

urlpatterns = [
  url(r'', include(router.urls)),
]

And in views.py:

class MyModelViewSet(ListModelMixin, GenericViewSet):
  serializer_class = MyModelSerializer

So I can send GET /mymodel/ request. I would like to be able to send PATCH /mymodel/, but I don't know how to configure the action. Currently I have:

class MyModelViewSet(ListModelMixin, GenericViewSet):
  serializer_class = MyModelSerializer
  allowed_http_methods = ['get', 'patch']

  @action(methods=['patch'], detail=False, url_path='', suffix='')
  def some_action(self, request: Request) -> Response:
    # do some stuff
    return Response()

However, I have 405 method not allowed response. I can send data with PATCH /mymodel/some-action/, but I need to get rid of the last part of the url path.

gonczor
  • 3,994
  • 1
  • 21
  • 46
  • Not really. I know I can perform DELETE/PATCH etc. methods on "detail" actions, but I want to do it on non-detail one and not add the action name at the end of the url. – gonczor May 27 '20 at 10:05
  • so in same api you want to have two path methods, first one like `PATCH api/my_api/` second one is `PARCH api/my_api/id/`. Right? Or you want to have only first one? – ncopiy May 27 '20 at 10:08
  • Only the first one. – gonczor May 27 '20 at 10:09
  • check out my response on question from first comment link – ncopiy May 27 '20 at 10:10
  • try to replace `delete` to `patch` in decorator and func name – ncopiy May 27 '20 at 10:13
  • Oh, sorry, I only noticed the accepted response. Changing the name to "patch" did the job. Thank you for help :) – gonczor May 27 '20 at 10:14

0 Answers0