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

2022-11-16 09:19 (2 years ago) ytyng

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)
Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011