Skip to content

Instantly share code, notes, and snippets.

@akkuman
Created October 29, 2020 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkuman/e2cdf463f0a891e318318d66916fc1b4 to your computer and use it in GitHub Desktop.
Save akkuman/e2cdf463f0a891e318318d66916fc1b4 to your computer and use it in GitHub Desktop.
drf中根据不同的action分发不同的权限
class WhiteListViewSet(ModelViewSet):
queryset = WhiteList.objects.all()
serializer_class = WhiteListSerializer
permission_classes = [IsAdminUser]
pagination_class = None
def get_permissions(self):
"""
根据不同的action获取不同的权限校验中间件
"""
if self.action == 'retrieve' or self.action == 'list':
permission_classes = []
else:
permission_classes = [IsAdminUser]
return [permission() for permission in permission_classes]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment