你可以使用
@action
装饰师,但是
detail=False
将url添加到视图集的“根”。
# views.py
class DashboardViewSet(viewsets.ViewSet):
@action(detail=False, url_path="download")
def get_download(self, request):
pass
# urls.py in your app
router = SimpleRouter()
router.register("dashboard", DashboardViewSet)
urlpatterns = router.urls
从你的例子来看,你可能只需要做一个单曲就行了
APIView
对于每个函数,然后使用path手动添加它们
class DashPendingView(APIView):
def get(self, request):
return Response(pending_things)
url_patterns = [
path("dashboard/pending/", DashPendingView.as_view(), "dash-pending")
]