Each user who needs access to the administrator must have the flag is_staff=True . It is never recommended that users who are not affiliated with your organization have access to an administrator. Seriously, just don't do it. If this is your plan, find another.
However, this can be done, but it is not for the faint of heart. There are so many. The first subclass is UserCreationForm and UserChangeForm by default (Auth uses two separate forms for it admin). Override the __init__ method for each of them to pull the request from kwargs (forms do not receive the request by default, but it is needed here, so you need to do a little workaround.) Then, by default subclass UserAdmin set form and add_form to new forms and override get_form (to go to request ) and each of the has_foo_permission methods to restrict access. The queryset method must also be overloaded, so users only see users that they can change in admin.
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.models import Group, Permission class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm.Meta): pass def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(CustomUserCreationForm, self).__init__(*args, **kwargs)
source share