Exclude Users with is_active = False in Django Admin (Replacing the User Admin)

2022-11-16 00:19 (3 years ago)
Exclude Users with is_active = False in Django Admin (Replacing the User Admin)

In UserAdmin, I will display only users with is_active = True.

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

class ReplacedUserAdmin(UserAdmin):
    list_display = (
        'id',
        'email',
        'username',
        'last_name',
        'first_name',
        'date_joined',
        'is_staff',
        'is_superuser',
    )
    ordering = ('-is_active', '-id',)

    def get_queryset(self, request):
        return super().get_queryset(request).filter(is_active=True)

# Replace UserAdmin.
admin.site.unregister(User)
admin.site.register(User, ReplacedUserAdmin)

When using the @admin.register decorator, it doesn't work well due to the order of code evaluation.

Write these two lines together:

admin.site.unregister(User)
admin.site.register(User, ReplacedUserAdmin)
Please rate this article
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive