Krisnadi
Krisnadi

Reputation: 681

Override django admin authentication

I'm using django 2.1.5. I want to override the django admin authentication process, the objective is to allow user with is_staff=False to be able to login. So, i'm trying to override the AdminSite and followed this docs (https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#overriding-default-admin-site). This is the code so far:


my_app/forms.py

from django.contrib.admin.forms import AdminAuthenticationForm

class CustomAdminAuthenticationForm(AdminAuthenticationForm):

    def confirm_login_allowed(self, user):
        super().confirm_login_allowed(user)   # i removed is_staff checking here

my_app/admin.py

class MyAdminSite(admin.AdminSite):
    login_form = CustomAdminAuthenticationForm

    def has_permission(self, request):
        return request.user.is_active   # i also removed request.user.is_staff here

my_app/apps.py

from django.contrib.admin.apps import AdminConfig

class MyAdminConfig(AdminConfig):
    default_site = 'my_app.admin.MyAdminSite'

root/settings.py

INSTALLED_APPS = [
    'my_app.apps.MyAdminConfig',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'my_app',
]

When i tried to runserver, there is an error:

ImportError: Module "my_app.admin" does not define a "MyAdminSite" attribute/class

I think i have point the default_site in my_app/apps.py correctly. How can i fix this? Is there any other way to complete my objective other than overriding the AdminSite? Thank you.

Upvotes: 1

Views: 2082

Answers (1)

Endre Both
Endre Both

Reputation: 5730

This seems like an awful lot of trouble instead of just assigning every user the staff role. The Django docs define the is_staff field in User as a boolean that "[d]esignates whether this user can access the admin site". Isn't this just what you want?

In other words: What else do you need is_staff for that prevents you from making all your users into staff members if you want them all to be able to access the admin site?

Upvotes: 3

Related Questions