DRF的@action装饰器

# 转自:http://www.cnblogs.com/zhzhlong/p/9325180.html

视图集中附加action的声明

from rest_framework.decorators import action  
# 追加action:返回书记的倒叙地0个书籍的信息
    @action(methods=['get'],detail=False)
    def latest(self, request):
        """
  # 追加action 修改图书的阅读数量
    @action(methods=['put'],detail=True)
    def read(self, request, pk):
        ...

其中:

@action()
action装饰器可以接收两个参数:
    methods: 声明该action对应的请求方式,列表传递
    detail: 声明该action的路径是否与单一资源对应,及是否是xxx/<pk>/action方法名/
        True 表示路径格式是xxx/<pk>/action方法名/
        False 表示路径格式是xxx/action方法名/
posted @ 2018-11-21 18:42  苦瓜爆炒牛肉  阅读(2969)  评论(0编辑  收藏  举报