Add custom route to viewsets.ModelViewSet
django, django-rest-framework
Solution
Yes, you can do that. Just add your method in the viewset with the `list_route` decorator.
from rest_framework.decorators import list_route
class SnippetViewSet(viewsets.ModelViewSet):
...
@list_route(renderer_classes=[renderers.StaticHTMLRenderer])
def highlight(self, request, *args, **kwargs):
...
It will add a url without the `pk` param like :
r'^snippets/highlight/$'
You can even specify the methods it supports using the `methods` argument in your decorator.
http://www.django-rest-framework.org/api-guide/routers/#usage
Problem
In the docs there is the example of methods with custom url: http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers ``` class SnippetViewSet(viewsets.ModelViewSet): ... @link(renderer_classes=[renderers.StaticHTMLRenderer]) def highlight(self, request, *args, **kwargs): snippet = self.get_object() return Response(snippet.highlighted) ``` This example add following route: ``` url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'), ``` It is possible to add an url without pk param, like this? ``` r'^snippets/highlight/$' ```