I would like to know if there is a better way to implement common permission that is very similar across django app. For example, app1
need permission_x
, app2
need permission_x
, and app3
need permission_x
too. In order to satisfy this i made permissions.py
and create permission_x
class which inherits from BasePermission
on each app.
app1/permissions.py
from rest_framework.permissions import BasePermission, SAFE_METHODS
class ReadOnly(BasePermission):
"""Object-level permission to only allow read-only operations."""
def has_permission(self, request, view):
# Read permissions are allowed to any request,
# hence allow GET, HEAD, or OPTIONS requests.
return request.method in SAFE_METHODS
app2/permissions.py
from rest_framework.permissions import BasePermission, SAFE_METHODS
class ReadOnly(BasePermission):
"""Object-level permission to only allow read-only operations."""
def has_permission(self, request, view):
# Read permissions are allowed to any request,
# hence allow GET, HEAD, or OPTIONS requests.
return request.method in SAFE_METHODS
app3/permissions.py
from rest_framework.permissions import BasePermission, SAFE_METHODS
class ReadOnly(BasePermission):
"""Object-level permission to only allow read-only operations."""
def has_permission(self, request, view):
# Read permissions are allowed to any request,
# hence allow GET, HEAD, or OPTIONS requests.
return request.method in SAFE_METHODS
As you can see, neither app1, app2, nor app3 need these basic permissions. They are exactly the same as each other. I implemented it as above, but is there a better way to do it? I tried looking and couldn't find anything that suits my needs, maybe create sub-app?