Overriding of perms_map
in subclasses of DjangoModelPermissions
#8756
-
Following use case: I have a bunch of views that use vanilla class V1(ModelViewSet):
permission_classes = (DjangoModelPermissions, )
... I want to add another view that should also respect view permissions. My naive approach was to:
Stub: class PermsWithView(permissions.DjangoModelPermissions):
def __init__(self):
self.perms_map['GET'] = ['%(app_label)s.view_%(model_name)s']
class V2(ModelViewSet):
permission_classes = (PermsWithView, )
... The problem here is that since In [1]: from rest_framework import permissions
In [2]: from my_stuff import PermsWithView
In [3]: permissions.DjangoModelPermissions.perms_map
Out[3]:
{'GET': [],
'OPTIONS': [],
'HEAD': [],
'POST': ['%(app_label)s.add_%(model_name)s'],
'PUT': ['%(app_label)s.change_%(model_name)s'],
'PATCH': ['%(app_label)s.change_%(model_name)s'],
'DELETE': ['%(app_label)s.delete_%(model_name)s']}
In [4]: my_perm = PermsWithView()
In [5]: permissions.DjangoModelPermissions.perms_map
Out[5]:
{'GET': ['%(app_label)s.view_%(model_name)s'],
'OPTIONS': [],
'HEAD': [],
'POST': ['%(app_label)s.add_%(model_name)s'],
'PUT': ['%(app_label)s.change_%(model_name)s'],
'PATCH': ['%(app_label)s.change_%(model_name)s'],
'DELETE': ['%(app_label)s.delete_%(model_name)s']} Is this an expected behaviour? Of course, it isn't hard to work around the limitation, e.g. by using class PermsWithView(permissions.DjangoModelPermissions):
def __init__(self):
self._perms_map = super().perms_map.copy()
self._perms_map['GET'] = ['%(app_label)s.view_%(model_name)s']
@property
def perms_map(self):
return self._perms_map but I'm curious whether the default behaviour is as designed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hmm, yeah actually just creating a copy is already sufficient: class PermsWithView(permissions.DjangoModelPermissions):
def __init__(self):
self.perms_map = super().perms_map.copy()
self.perms_map['GET'] = ['%(app_label)s.view_%(model_name)s'] This question looks like just noise to me now, as this is just how Python behaves and not really a DRF issue. |
Beta Was this translation helpful? Give feedback.
Hmm, yeah actually just creating a copy is already sufficient:
This question looks like just noise to me now, as this is just how Python behaves and not really a DRF issue.