[英]How to use logical operators in DRF's DEFAULT_PERMISSION_CLASSES?
Since DRF==3.9--(release notes) we have got an option to combine/compose the permission classes in our views.由于DRF==3.9--(发行说明) ,我们可以选择在我们的视图中组合/组合权限类。
class MyViewSet(...):
permission_classes = [
FooPermission & BarPermission]
I did try something like this,我确实尝试过这样的事情,
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'utils.permissions.FooPermission' & 'utils.permissions.BarPermission',
),
# other settings
}
and python raised exception和 python 引发异常
TypeError: unsupported operand type(s) for &: 'str' and 'str'
类型错误:&:“str”和“str”不支持的操作数类型
So,所以,
How can I use combined permission as global permission using DEFAULT_PERMISSION_CLASSES
?如何使用
DEFAULT_PERMISSION_CLASSES
将组合权限用作全局权限?
I created a new variable by combining those classes and referenced the same in the DEFAULT_PERMISSION_CLASSES
,我通过组合这些类创建了一个新变量,并在
DEFAULT_PERMISSION_CLASSES
中引用了相同的变量,
# utils/permissions.py
from rest_framework.permissions import BasePermission
class FooPermission(BasePermission):
def has_permission(self, request, view):
# check permissions
return ...
class BarPermission(BasePermission):
def has_permission(self, request, view):
# check permissions
return ...
CombinedPermission = FooPermission & BarPermission
# settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'utils.permissions.CombinedPermission',
),
# other settings
}
&
in this example.&
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.