Django: display user group in admin

I would like to change the administrator for the group so that it displays the number of users in a specific group. I would like to display this in a view showing all the groups, and before you type admin for a specific group. Is it possible? I am talking about how to change the administrator for a group and how to add a function to list_display.

+5
source share
1 answer

First you need to import and subclass GroupAdminfrom django.contrib.auth.admin. In your subclass, define a method user_count. Then unregister the existing group model from the administrator and re-register the new one.

from django.contrib.auth.admin import GroupAdmin
from django.contrib.auth.models import Group

class GroupAdminWithCount(GroupAdmin):
    def user_count(self, obj):
        return obj.user_set.count()

    list_display = GroupAdmin.list_display + ('user_count',)

admin.site.unregister(Group)
admin.site.register(Group, GroupAdminWithCount)
+8

All Articles