Django-RESTフレームワークでクラスベースのビューで特定のメソッドに権限クラスを適用する方法

StackOverflow https://stackoverflow.com//questions/22010675

  •  21-12-2019
  •  | 
  •  

質問

「POST」の方式でのみ権限を適用したい場合は

class Signup(APIView):
    def post(self, request, format=None):
        something...something

    @authentication_classes((ExpiringTokenAuthentication,))
    @permission_classes((IsAuthenticated,))
    def put(self, request, format=None):
        something...something
.

役に立ちましたか?

解決

権限クラスhas_permissionメソッドでHTTPメソッドを確認し、それがPUTの場合はチェックを適用します。

class ExpiringTokenAuthentication(permissions.BasePermission):

    def has_permission(self, request, view):
        if request.method == 'PUT':
           # do it here


class Signup(APIView):
    permission_classes = (BlacklistPermission,)
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top