参数化权限类的明智方法是将参数放在视图类上。这将让您从一个视图更改为另一个视图的行为。
这是一个例子:
# permissions.py
from django.contrib.auth.models import Group
from rest_framework import permissions
def is_in_group(user, group_name):
"""
Takes a user and a group name, and returns `True` if the user is in that group.
"""
try:
return Group.objects.get(name=group_name).user_set.filter(id=user.id).exists()
except Group.DoesNotExist:
return None
class HasGroupPermission(permissions.BasePermission):
"""
Ensure user is in required groups.
"""
def has_permission(self, request, view):
# Get a mapping of methods -> required group.
required_groups_mapping = getattr(view, "required_groups", {})
# Determine the required groups for this particular request method.
required_groups = required_groups_mapping.get(request.method, [])
# Return True if the user has all the required groups or is staff.
return all([is_in_group(request.user, group_name) if group_name != "__all__" else True for group_name in required_groups]) or (request.user and request.user.is_staff)
然后,您可以HasGroupPermission
像这样使用该类:
# views.py
class MyView(APIView):
permission_classes = [HasGroupPermission]
required_groups = {
'GET': ['moderators', 'members'],
'POST': ['moderators', 'someMadeUpGroup'],
'PUT': ['__all__'],
}
...
希望有帮助!