您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Django Rest Framework中为特定请求添加权限

5b51 2022/1/14 8:20:27 python 字数 1494 阅读 498 来源 www.jb51.cc/python

我正在为文档应用程序创建基于类的API,但我想在APIView中为帖子和补丁定义添加特定权限.例如, class DocumentList(APIView): def get(self,request,format=None): ... blah def post(self,request,format=None): only allow ad

概述

class DocumentList(APIView):

    def get(self,request,format=None):
         ... blah

    def post(self,format=None):
        only allow administrators to create new documents 
        ... blah
# settings.py
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated','rest_framework.permissions.DjangoModelPermissions'
    )
}

# views.py
class DocumentList(APIView):
    model = Document
    ...

DjangoModelPermissions权限映射can be found in the source.

> GET,OPTIONS和HEAD不需要许可,但由于我们指定了IsAuthenticated,我们仍然要求> POST地图添加> PUT和PATCH地图改变>删除删除的地图

总结

以上是编程之家为你收集整理的如何在Django Rest Framework中为特定请求添加权限全部内容,希望文章能够帮你解决如何在Django Rest Framework中为特定请求添加权限所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶