Looking over here: http://www.django-rest-framework.org/api-guide/routers/#extra-link-and-actions The example has pk=None
:
@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
def set_password(self, request, pk=None):
and says the following url pattern will be generated: ^users/{pk}/set_password/$
Over here: http://www.django-rest-framework.org/api-guide/routers/#simplerouter It also says that lookup
field is in the url: {prefix}/{lookup}/{methodname}/
Does DRF pass the lookup
value to the detail_route
method? Based on the documentation, it looks like it does, but over here: http://www.django-rest-framework.org/api-guide/routers/#example
If you scroll down to:
@detail_route()
def group_names(self, request):
"""
Returns a list of all the group names that the given
user belongs to.
"""
user = self.get_object()
groups = user.groups.all()
return Response([group.name for group in groups])
A lookup value is not asked for as an argument to the group_names
function. My questions are:
1) Is pk
/ a lookup field argument required in a detail_route
?
2) If not, then how does self.get_object()
know which object to get?
3) Also, if not, then shouldn't @list_route
be used instead since a lookup is not even being used?
Edit: Over here: http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing It also says The @detail_route decorator contains pk in its URL pattern and is intended for methods which require a single instance.
1) Is pk / a lookup field argument required in a detail_route?
Yes. Thanks for spotting it, it's fixed in the repository and will be updated next time the documentation will be generated.
2) If not, then how does self.get_object() know which object to get?
The view's arguments / keyword arguments is stored in the view.args
and view.kwargs
which get_object uses.
3) Also, if not, then shouldn't @list_route be used instead since a lookup is not even being used?
It's been used through self.get_object which returns the associated users so it's a detail_route.