DRF detail route without a PK
<p>I’d like to create a detail route that renders the data of the current user.</p>

<p>If I use a custom router:</p>

<pre><code>class CustomRouter(routers.DefaultRouter):
    def __init__(self, *args, **kwargs):
        super(CustomRouter, self).__init__(*args, **kwargs)

        self.routes.append(
            routers.Route(url=r'^{prefix}/current{trailing_slash}',
                          mapping={'get': 'current'},
                          name='{basename}-current',
                          initkwargs={'suffix': 'Detail'}))


rest_router = CustomRouter()
urlpatterns = [
    url(r'^', include(rest_router.urls)),
]
</code></pre>

<p>it works, and I can access my logged in user at <code>/api/users/current/</code>.</p>

<p>However, if I use my <code>CustomUser</code> and register the my whole <code>UserViewSet</code> (which descends from <code>viewsets.ReadOnlyModelViewSet</code>):</p>

<pre><code>rest_router.register(r'users', viewsets.UserViewSet)
</code></pre>

<p>my <code>current/</code> route stops working. Is there a way I can add such a custom detail route that doesn’t require a pk?</p>

<p><strong>Edit</strong>: my second approach was to add a <code>@list_route</code> that actually returns only one object instead of one. This doesn’t require a custom router at all, just an extra method on my <code>UserViewSet</code>:</p>

<pre><code>class UserViewSet(viewsets.ReadOnlyModelViewSet):
    …
    @list_route(methods=['get'])
    def current(self, request):
        return Response(self.serializer_class(request.user).data)
</code></pre>

<p>This, however, is pretty ugly for obvious reasons: why would I return one object in a list route?</p>