DRF视图集中添加其他API--@action装饰器
问题:DRF视图集中若想添加其他API?
-
url_name:指定url名称,默认为被action修饰的函数名,内部操作用
注意:detail为必填参数,显示定义的路由优先级大于默认的
例1:detail=False
class MenuViewSet(BaseModelViewSet): """ 菜单管理API """ queryset = Menu.objects.all() serializer_class = MenuSerializer # 获取用户菜单/菜单按钮权限API # detail详情大意为是否需要传pk,若传为True,反之为False @action(methods=['GET'], detail=False, url_path='web_router') def xingchen(self, request): return ResponseOK(msg="获取成功")
例2:detail=True
class MenuViewSet(BaseModelViewSet): """ 菜单管理API """ queryset = Menu.objects.all() serializer_class = MenuSerializer @action(methods=['GET'], detail=True) def xc(self, request, pk): print('id', pk) return ResponseOK(msg="获取成功")
本文来自博客园,作者:手可摘星辰/*,转载请注明原文链接:https://www.cnblogs.com/u-damowang1/p/16990699.html