'How to check if the logged in user is present in groups in class based views(viewsets.ModelViewSet)?
I have a class based view
class JobViewSet(viewsets.ModelViewSet):
queryset = Job.objects.all()
serializer_class = JobSerializer
permission_classes = (IsAuthenticated,)
I have 2 group of users(implemented using django.contrib.auth.models Group), 'company' and 'customer'. On each view, I have to check if the user belongs to a certain group. Is it possible to do it using custom permission_classes.
Thank you.
Solution 1:[1]
Yes you can. You can see that here Something like :
class IsCompany(permissions.BasePermission):
"""
Check if user is in company group
"""
def has_permission(self, request, view):
user = request.user
is_company = user.groups.filter(name='company')
return is_company
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sami Tahri |
