0

I'm using viewsets.ModelViewSet and want to replace the standard endpoints URLs
for example:
instead of creating new snippet with the "standard" endpoint
POST {BAST_URL}/snippet/
I want to replace it with "create" URL and disabling the standard
POST {BAST_URL}/snippet/create/

I able to create a new custom create method but not to
* use "create" in the URL -> ERROR: Cannot use the @action decorator on the following methods, as they are existing routes: create
* Disabling Standart URL from creating a snippet

@action(detail=False, methods=['post'])
def create_snippet(self, request, *args, **kwargs):
    return super(SnippettViewSet, self).create(request, *args, **kwargs)
Yocheved Z
  • 25
  • 4

1 Answers1

1

you need to pass an extra argument url_path to the @action decorator like below

@action(detail=False, methods=['post'], url_path='snippet/create', url_name='snippet_create')
def snippet(self, request, *args, **kwargs):
    return super(SnippettViewSet, self).create(request, *args, **kwargs)
dipesh
  • 763
  • 2
  • 9
  • 27
  • Thanks! Can I also disable the standard create method? POST {BAST_URL}/snippet/ – Yocheved Z Apr 02 '20 at 10:59
  • You can use the `GenericViewSet` and define all the actions by yourself using the `@action` decorator. – dipesh Apr 02 '20 at 11:08
  • I tried do it and still get the error _Cannot use the @action decorator on the following methods, as they are existing routes: create_ – Yocheved Z Apr 02 '20 at 11:34
  • You can add `url_name` as an argument in the `@action` decorator so that you can use any method name. I've edited the answer, please check. – dipesh Apr 02 '20 at 11:40