Django Rest Framework use DjangoModelPermissions on ListAPIView
@Yannic Hamann's solution has a small bug. It overwrites parent's perms_map['GET'].
As follows, A dictionary overring need deepcopy.
class CustomDjangoModelPermission(permissions.DjangoModelPermissions):
def __init__(self):
self.perms_map = copy.deepcopy(self.perms_map) # you need deepcopy when you inherit a dictionary type
self.perms_map['GET'] = ['%(app_label)s.view_%(model_name)s']
dictionary overring test
class Parent:
perms = {'GET':'I am a Parent !'}
class Child(Parent):
def __init__(self):
self.perms['GET'] = 'I am a Child !'
dictionary overring result
>>> print(Parent().perms['GET'])
I am a Parent !
>>> print(Child().perms['GET'])
I am a Child !
>>> print(Parent().perms['GET'])
I am a Child ! # Parent's perms is overwritten by Child.
^^^^^
Ouch, that was easier than I thought:
class CustomDjangoModelPermission(permissions.DjangoModelPermissions):
def __init__(self):
self.perms_map = copy.deepcopy(self.perms_map) # from EunChong's answer
self.perms_map['GET'] = ['%(app_label)s.view_%(model_name)s']