[Fixed]-Has_permission() missing 1 required positional argument: 'view'

33๐Ÿ‘

โœ…

You are missing a class instantiation for permissions.IsAuthenticated:

def get_permissions(self):
     if self.request.method in permissions.SAFE_METHODS:
         return (permissions.AllowAny(),)
     return (permissions.IsAuthenticated, IsAuthorOfPost(),)
#                                      ^^^

The error message comes from calling the instance method on IsAuthenticated on the class. Thus request gets mapped to self, view to request and view itself is then missing.

Changing get_permissions() to

def get_permissions(self):
     if self.request.method in permissions.SAFE_METHODS:
         return (permissions.AllowAny(),)
     return (permissions.IsAuthenticated(), IsAuthorOfPost(),)
#                                       ^^

should solve the problem.

As a side note: Your get_permissions() code takes an active role in deciding authorization. It would be better to move this functionality into the permissions themselves to make the code better follow the single responsibility principle.

๐Ÿ‘คdhke

0๐Ÿ‘

In my case, I had written def instead of class while defining the class that inherits from BasePermission

Leave a comment