For anyone who search a similar solution, I actually use this inside my Viewset using some Heritage :
For admin using private ADMIN API
READ all users data if there are in correct groups
filter_backends = [DjangoFilterBackend]
permission_classes = [
(HasGroupPermission | permissions.IsAdminUser)
& permissions.IsAuthenticated
]
required_groups = {
"GET": ["administrator"],
"POST": ["administrator"],
"PUT": ["administrator"],
"DELETE": ["administrator"],
}
For users using public USER API
READ owner data and set action permissions using ObjectPermissions from django-rest-framework-guardian
user_perms_assign_all() is just a function that assign all perms using from guardian.shortcuts import assign_perm
"""
Viewset that only list events if user has 'view' permissions => filter_backends
Only allows operations on individual event if user has appropriate 'view', 'add',
'change' or 'delete' permissions => ObjectPermissions
view/create/update/delete permissions are set with perform create using user_perms_assign_all()
"""
permission_classes = [
ObjectPermissions & permissions.IsAuthenticated
]
filter_backends = [filters.ObjectPermissionsFilter]
def perform_create(self, serializer):
"""
If API call create multiples objects and want to set perms for each, need to overrite this method to loop inside nested objects
"""
object_instance = serializer.save()
user_perms_assign_all(self.request.user, object_instance)
Fell free to show me different approach or correction
I let all permissions for owner but restrict USER public api inside viewset again with :
http_method_names = [
"get",
"post",
"put",
"patch",
"delete",
"head",
"options"
]