如何在视图集中添加自定义权限 [英] How to add custom permission in viewset

查看:150
本文介绍了如何在视图集中添加自定义权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建模块时,如何在Django rest框架的视图集中添加默认权限以外的自定义权限?
我有一个权限 fix_an_appointment。在下面的视图集中,如何包括此权限?拥有此权限的人只能创建。

How to add custom permission in viewset in django rest framework other than the default permission while creating a module? I have a permission "fix_an_appointment". In the below viewset, how to include this permission? Those who have this permission has only able to create.

我的views.py文件:

My views.py file:

class settingsViewSet(viewsets.ModelViewSet):
    serializer_class = SettingsSerializer
    queryset = Setting.objects.all()

有人可以帮忙吗?

推荐答案

我不能使用装饰器,例如: @permission_classes(IsAuthenticated ,)在ViewSet中的其他操作中

I can't use a decorator like: @permission_classes(IsAuthenticated, ) in extra actions within ViewSet

要在操作中使用不同的权限,请将其放入 @action ()作为参数。

To use different permissions in actions, instead, put it into the @action() as a parameter.

@action(detail=True, methods=['post'], permission_classes=[IsAdminOrIsSelf])
    def set_password(self, request, pk=None):
       ...

drf doc

这篇关于如何在视图集中添加自定义权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆