I have following code in my MyModuleViewSet
:
class MyModuleViewSet(viewsets.ModelViewSet):
# ...
@action(detail=False, permission_classes=(IsOwnerOrReadOnly,))
@debugger_queries
def my_rest_api(self, request, pk=None):
# ...
return HttpResponse(resp, content_type="application/json", status=status.HTTP_200_OK)
It is registered in my urls.py
as follows:
router.register(r'mymodule', MyModuleViewSet)
I tried to hit this end point with following link:
http://0.0.0.0:3000/myproject/api/mymodule/1019/my_rest_api/?format=json¶m1=7945
But it gave error saying:
Page not found (404)
Request Method: GET
Request URL: http://0.0.0.0:3000/myproject/api/mymodule/1019/my_rest_api/?format=json¶m1=7945
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
1. ...
2. ...
...
37. courseware/ api/ ^mymodule/my_rest_api/$ [name='mymodule-my-rest-api']
38. courseware/ api/ ^mymodule/my_rest_api\.(?P<format>[a-z0-9]+)/?$ [name='mymodule-my-rest-api']
...
As you can see mymodule/my_rest_api
is indeed specified in the response on line 37 and 38. So, I tried with different url by removing /1019
:
http://0.0.0.0:3000/myproject/api/mymodule/1019/my_rest_api/?format=json¶m1=7945
and it hit the breakpoint inside the rest endpoint method. I was thinking that 1019
will get assigned to pk
argument inside my_rest_api(self, request,pk=None)
. But that did not happen. Why is this the case? Did I miss some basic understanding of how DRF works?
if your action looks like this
@action(detail=False, permission_classes=(IsOwnerOrReadOnly,))
your url can't be
http://0.0.0.0:3000/myproject/api/mymodule/1019/my_rest_api/
set
detail=True
and add pk indef my_rest_api(self, pk, request):
or remove id from your url to
http://0.0.0.0:3000/myproject/api/mymodule/my_rest_api/