Using the permission class on a detailed route

How can I apply the permission class only to the detailed route?

class EventViewSet(viewsets.ModelViewSet): @detail_route(methods=['post']) def messages(self, request, pk=None): ### Check a permissions class. ... 
+7
django django-rest-framework
source share
2 answers

You can add permissions basically by doing this:

 class EventViewSet(viewsets.ModelViewSet): @detail_route( permission_classes=[ permissions.PermissionClass_], methods=['post']) def messages(self, request, pk=None): ### Check a permissions class. ... 
+13
source share

If you have a problem with permissions_classes in your custom actions in the ViewSet, try using this decorator for your action. Probably the new Django Rest Framework is not considering permissions. The solution to this situation is to check it yourself at the beginning of each user action or use the following decorator:

 def check_permissions(fun): def ref(self, request, pk=None): obj = get_object_or_404(self.get_queryset(), pk=pk) self.check_object_permissions(self.request, obj) return fun(self, request, pk) return ref 
0
source share

All Articles