Skip to content Skip to sidebar Skip to footer

Django Admin Is_staff Based On Group

Is it possible to have is_staff selected by choosing a group? Let's say there are two groups: users, admins When a new user is in the users group he is not staff, but if he is in t

Solution 1:

There is an easy way to do this define the following in your user model

@propertydefis_staff(self):
    if self.is_staff == Trueor self.groups.filter(name="staff").exists()

Thus during admin login or any other time when you call from the user_object.is_staff You will be getting what you want on basis of groups too.

Solution 2:

I managed to make it work by extending the UserAdmin class and in the get_form function I placed this with help of mascot6699's answer:

if obj.groups.filter(name="Administrator").exists():
    obj.is_staff = Trueelse:
    obj.is_staff = False

So whenever I place a user (with the admin menu) in the Administrator group it will check the is_staff option else it unchecks it.

Solution 3:

The is_staff property is primarily used by the admin interface. If you want to have an admin interface that's dependent on group membership, you can override AdminSite.has_permission() instead:

classGroupBasedAdminSite(admin.AdminSite):
    defhas_permission(self, request):
        return request.user.is_active and request.user.groups.filter(name = 'admins').exists()

# override default admin site
admin.site = GroupBasedAdminSite()

You can also use the official override feature, or have a dedicated GroupBasedAdminSite hosted on a different path, in case you want to support different types of "admins".

Solution 4:

There are two place one should override to implement this behaviour

# inside any app's admin.py moduleimport types

from django.contrib import admin
from django.contrib.admin.forms import AdminAuthenticationForm

defhas_permission(self, request):
    return request.user.is_active and (
        request.user.is_staff
        or request.user.groups.filter(name="grp").exists()
    )


classGrpAdminAuthenticationForm(AdminAuthenticationForm):
    defconfirm_login_allowed(self, user):
        if user.groups.filter(name="grp").exists():
            user.is_staff = Truesuper().confirm_login_allowed(user)


admin.site.login_form = GrpAdminAuthenticationForm
admin.site.has_permission = types.MethodType(has_permission, admin.site)

It will update the default admin.site object so one doesn't need to register to a custom object.

Post a Comment for "Django Admin Is_staff Based On Group"