Django REST Framework viewset per-action permissions

前端 未结 5 987
忘掉有多难
忘掉有多难 2020-12-13 00:38

Is there a best practice to assign a different permission to each action of a given APIView or ViewSet?

Let\'s suppose I defined some permis

5条回答
  •  不思量自难忘°
    2020-12-13 00:49

    You can create a custom permission class extending DRF's BasePermission.

    You implement has_permission where you have access to the request and view objects. You can check request.user for the appropriate role and return True/False as appropriate.

    Have a look at the provided IsAuthenticatedOrReadOnly class (and others) for a good example of how easy it is.

    I hope that helps.

提交回复
热议问题