2

I am using the django admin site for my web app, but i am having a problem. I need that the staff users can change, create and delete another staff users, but i don't want that they change the informations of the superusers. I want to know if is possible filter the user list by role (the staff user don't see the superusers in the list).

eduCan
  • 180
  • 1
  • 9
  • I would suggest better have role or privilage option in db while user signing up and for all admin signup privilage will be admin and for normal user by default user . This will help you extent as much as you want letter – jayprakashstar Jun 01 '15 at 13:58

2 Answers2

4

Finally I found how to do this, I leave the code here just in case someone hav the same problem that I had

def get_queryset(self, request):
    queryset = super(UserAdmin, self).get_queryset(request)
    if request.user.is_superuser:
        return queryset
    return queryset.filter(is_superuser=False)
Community
  • 1
  • 1
eduCan
  • 180
  • 1
  • 9
0

You will need to create a custom ModelAdmin for the User model. I recommend you to inherit from the original one and then you can override the get_queryset method.

You should end with:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):

    def get_queryset(self, request):
        qs = super(MyUserAdmin, self).get_queryset(request)
        if request.user.is_superuser:
             return qs
        else:
             return qs.filter(is_superuser=False)

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
argaen
  • 4,145
  • 24
  • 28