Finally, I found a way to solve this problem.
Here's a very elegant way to do this, by rewriting the get_queryset function in my UserViewSet:
class UserViewSet(viewsets.ModelViewSet):
In change 1, permissions allowed someone to access, so a new user can perform POST without any authentication.
In change 2, I return all users only when the user is superuser, as rewritten by get_queryset done.
You also need to modify the urls.py file to add the base_name for this URL, for example:
router.register(r'users', UserViewSet, base_name='user')
ref, stack overflow
Belter
source share