看这里: http: //www.django-rest-framework.org/api-guide/routers/#extra-link-and-actions这个例子有pk=None
:
@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
def set_password(self, request, pk=None):
并说将生成以下 url 模式:^users/{pk}/set_password/$
在这里: http: //www.django-rest-framework.org/api-guide/routers/#simplerouter它还说该lookup
字段在 url 中:{prefix}/{lookup}/{methodname}/
DRF 是否将lookup
值传递给detail_route
方法?根据文档,它看起来确实如此,但在这里: http: //www.django-rest-framework.org/api-guide/routers/#example
如果您向下滚动到:
@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])
不要求查找值作为group_names
函数的参数。我的问题是:
1) pk
/ 中是否需要查找字段参数detail_route
?
2)如果不是,那么如何self.get_object()
知道要获取哪个对象?
3)另外,如果没有,那么不@list_route
应该使用,因为甚至没有使用查找?
编辑:在这里: http: //www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing它还说The @detail_route decorator contains pk in its URL pattern and is intended for methods which require a single instance.