Django Rest框架 - @detail_route的详细页面 [英] Django Rest Framework - detail page of @detail_route
问题描述
我在我的viewsets.ModelViewSet上使用@detail_route。
I am ussing @detail_route on my viewsets.ModelViewSet.
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
serializer_class = serializers.CompanySerializer
@detail_route(methods=['get', ], permission_classes=[IsCompanyUserPermission, ])
def accounts(self, request, pk):
...
return Response(...)
# urls.py
router.register(r'companies', views.CompanyViewSet)
此代码创建url:
/companies/
/companies/{id}
/companies/{id}/accounts
我不知道如何添加路线/视图来详细显示帐户:
I dont know how to add route/view to detail account:
/companies/{id}/accounts/{id_account}
有没有办法添加路线和视图来处理这条路线?
Is there any way to add route and views to handle this route ?
(最好的选择将在CompanyViewSet中添加)
(the best option would be add this on CompanyViewSet)
干杯,
推荐答案
DRF本身不处理嵌套路由,您可以手动处理或使用扩展名,如 drf-nested-router ,但其过时。
DRF does not handle nested routes by itself, you may handle it by hand or use an extension, like drf-nested-routers, but its outdated.
我的建议:不要打框架,DRF不好用网址嵌套资源,另一种做法。
My advice : don't fight the framework, DRF is not good at playing with url-nested resources, do it another way.
这篇关于Django Rest框架 - @detail_route的详细页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!