Search code examples
django-rest-frameworkdjango-rest-viewsets

Change standard endpoints urls Django Rest Framewrok


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)

Solution

  • 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)