我有一个 DRF View 集,想提供详细信息并列出具有相同名称的路由:
class MyViewSet(ViewSet):
# ...
@detail_route(['post'])
def do_stuff(self, request, *args, **kwargs):
pass
@list_route(['post'])
def do_stuff(self, request, *args, **kwargs):
pass
这显然行不通,因为 list_route
只会覆盖 detail_route
。
有没有一种简单的方法可以在不使用 manually messing up with the urls 的情况下提供两条路线? ?
最佳答案
我不确定这是否是您要找的,您可以为两者设置相同的 url 路径名:
class MyViewSet(ViewSet):
# ...
@detail_route(['post'], url_path='do_stuff')
def do_stuff_detail(self, request, *args, **kwargs):
pass
@list_route(['post'], url_path='do_stuff')
def do_stuff_list(self, request, *args, **kwargs):
pass
在第一个中,url 将是 ../"id"/do_stuff,在第二个中,url 将是 ../do_stuff
关于python - Django 休息框架 : @detail_route and @list_route with same name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178397/