Thursday 25 August 2022

pass a variable between multiple custom permission classes in drf

I have a base permission class that two ViewSets are sharing and one other permission class each that is custom to each of the ViewSets, so 3 permissions all together, is there a way to pass a specific variable down from the base permission class to the other permission classes? My setup looks like this:

class BasePerm(permissions.BasePermission):
    def has_permission(self, request, view):
        some_var = # call an API using request variable

class Perm1(permissions.BasePermission):
    def has_permission(self, request, view):
        # get the value of some_var from BasePerm


class Perm2(permissions.BasePermission):
    def has_permission(self, request, view):
        # get the value of some_var from BasePerm


class MyViewSet1(mixins.CreateModelMixin, viewsets.GenericViewSet):
    permission_classes = [BasePerm, Perm1]


class MyViewSet2(mixins.CreateModelMixin, viewsets.GenericViewSet):
    permission_classes = [BasePerm, Perm2]


from pass a variable between multiple custom permission classes in drf

No comments:

Post a Comment