I have a Django project with API based on the Django REST framework.
Recently I've enabled object-level permissions with the use of django-guardian. However I'm not using model-level permissions framework in my project yet (and until this moment I didn't see any purpose to.)
Now when I turn on DjangoObjectPermissions in my API viewset,
class PhotoViewSet(viewsets.ModelViewSet)
permission_classes = (permissions.DjangoObjectPermissions,)
and make an update request to the endpoint for an object that has a proper "change" permission from the Guardian framework, I get a 403 FORBIDDEN
response.
The reason for this response appears to lay in a request dispatch stage:
- DRF checks model permissions in this case.
DjangoObjectPermissions
inherits fromDjangoModelPermissions
.- DRF calls
DjangoObjectPermissions.has_permission()
has_permission()
fails because I'm not using model-level permissions.
What is a proper way to use DjangoObjectPermissions
in my case?
- (A) Implement a custom permission class, inherit it from
DjangoObjectPermissions
and overridehas_permission()
. - (B) Turn the Django's model-level permission framework on just for the sake of it.
- (C) Use this
hackworkaround to skip the check.