It gives you filter by staff status and superuser status, but what about groups?
4 Answers
Since version 1.3 it can be done using this:
list_filter = ('groups__name')
Of course as @S.Lott explains you must register your customized class in the admin.py file:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
list_filter = UserAdmin.list_filter + ('groups__name',)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

- 23,869
- 5
- 48
- 64
-
1Nice, thanks! But it shows up as "By name" followed by the list of groups, which is a bit confusing, it would be better if it said "By group" – thnee Apr 13 '13 at 19:15
-
I agree with @thnee. It can be achieved like this `list_filter = ('groups',)` – laltin Dec 06 '15 at 22:07
See Customizing an Admin form in Django while also using autodiscover
Essentially, you define a customized Admin class with the features you want.
Then unregister and register your revised Admin class.
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
Here is a complete example, that inherits from SimpleListFilter, which is available in Django 1.4 and up (current as of May 2023):
https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
It support setting all available labels and parameters to create the completely custom filter.
It shows up as "By group" in the filter panel, with a list of all available groups.
from django.contrib.admin import SimpleListFilter
from django.contrib.auth.models import Group
from django.utils.translation import ugettext as _
class GroupListFilter(SimpleListFilter):
title = _('group')
parameter_name = 'group'
def lookups(self, request, model_admin):
items = ()
for group in Group.objects.all():
items += ((str(group.id), str(group.name),),)
return items
def queryset(self, request, queryset):
group_id = request.GET.get(self.parameter_name, None)
if group_id:
return queryset.filter(groups=group_id)
return queryset
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
list_filter = UserAdmin.list_filter + (GroupListFilter,)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

- 193
- 3
- 8

- 5,817
- 3
- 27
- 23
In later versions of Django, it works exactly as you'd expect:
list_filter = ('groups', )
No need to unregister/register the admin class.

- 14,712
- 8
- 89
- 89
-
-
-
But you don't get a user-customizable admin.py for `django.contrib.auth` so a good place to do this override is in your custom User model's admin.py. – shacker May 28 '19 at 07:38
-
-
1Ah! In that case you can do it in any of your models, using the unregister/register techniques above. I was only recommending your User model because I assumed you had one, but Django actually doesn't care where you do it. – shacker May 30 '19 at 06:13