marcosgue
marcosgue

Reputation: 697

Django - Is there any way to hide superusers from the user list?

(sorry for my bad english)

I like to know if exist any way to hide the superusers from the users list in django admin, If the user that is viewing the list is not a superuser?

Thanks!

Upvotes: 3

Views: 1569

Answers (1)

Aamir Rind
Aamir Rind

Reputation: 39719

You can override get_queryset method of User Admin and only return results based on logged in user:

myapp/admin.py

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

@admin.register(User)
class UserAdmin(BaseUserAdmin):
    def get_queryset(self, request):
        qs = super(UserAdmin, self).get_queryset(request)
        if not request.user.is_superuser:
            return qs.filter(is_superuser=False)
        return qs

Upvotes: 6

Related Questions