How to make DRF use url path as empty and base path?
I am trying to omit the URL path for an action in a ViewSet like
@action(detail=False, methods=['patch'], url_path='')
def update_current_user(self, request):
But when I give url_path
as an empty string, DRF defaults to the function name and looks for the route at /api/current-user/update_current_user
. I want it to look at /api/current-user
.
How to drop the whole path for the function and use the base path?
This is an instance of a so called "Singleton Endpoint" pattern, where there is only a single instance of the resource is present - no lists. This is implemented by giving an empty base path for the resource and handling the resource path in the ViewSet.
router.register(r"", CurrentUserViewSet, basename="current-user")
class CurrentUserViewSet(viewsets.ViewSet):
...
@action(detail=False, methods=["patch"], url_path="current-user", url_name="current-user")
def current_user(self, request):
...
if request.method == 'GET':
...
elif request.method == 'PATCH':
...