class ActionPermissions(DjangoModelPermissions):
def has_permission(self, request: Request, view: GenericViewSet) -> bool:
if view.action == "custom_perm":
return request.user.has_perm("actions.custom_perm")
return super().has_permission(request, view) <-- `view` has APIView signature
after changing to make my code match your typing:
class ActionPermissions(DjangoModelPermissions):
def has_permission(self, request: Request, view: APIView) -> bool:
if view.action == "custom_perm": <-- view has no attribute 'action'
return request.user.has_perm("actions.custom_perm")
return super().has_permission(request, view)
I made some research and found that action attribute is created in ViewSetMixin in drf.
If my viewset inherits from GenericViewSet, I think I should be able to perform any operation related to the action field in the has_permission method.
Bug report
What's wrong
I wanted to add types for this code:
after changing to make my code match your typing:
I made some research and found that action attribute is created in
ViewSetMixin
in drf. If my viewset inherits from GenericViewSet, I think I should be able to perform any operation related to the action field in thehas_permission
method.My viewset looks:
How is that should be
Not sure, but maybe something with Generic type to determine exaclty incoming type?
or add a possible GenericViewSet type?
System information
python
version: 3.11.6django
version: 5.0mypy
version: 1.71django-stubs
version: 4.2.6