我想在每个方法中检查用户的权限,因此,在调用到达方法处理程序之前,必须已经检查了权限(DRY)。根据文档initial
使我能够做到这一点,但这是一个好习惯吗?
class StorageDetail(APIView):
def initial(self, request, *args, **kwargs):
if not has_permission(request):
return Response(status=status.HTTP_403_FORBIDDEN)
super(StorageDetail, self).initial(request, *args, **kwargs)
def post(self, request, storage_id):
# ....
def put(self, request, storage_id):
# ...
答案 0 :(得分:0)
没有。这不是一个好习惯。根据{{3}},最好使用Permission类。
from rest_framework import permissions
class CustomerAccessPermission(permissions.BasePermission):
message = 'Adding customers not allowed.'
def has_permission(self, request, view):
return True
class ExampleView(APIView):
permission_classes = (IsAuthenticated, CustomerAccessPermission,)