Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: displaying group users count in admin

I would like to change admin for a group, so it would display how many users are there in a certain group. I'd like to display this in the view showing all groups, the one before you enter admin for certain group. Is it possible? I am talking both about how to change admin for a group and how to add function to list_display.

like image 879
gruszczy Avatar asked Mar 24 '10 13:03

gruszczy


People also ask

What is staff status in Django admin?

staff. - A user marked as staff can access the Django admin. But permissions to create, read, update and delete data in the Django admin must be given explicitly to a user. By default, a superuser is marked as staff.

What are groups in Django admin?

Groups are a means of categorizing users. This allows for granting permissions to a specific group.

How do I show all columns in Django admin?

To display both the three columns in the admin site model list page, you need edit the Django app's admin.py file ( dept_emp / admin.py ), then define a class which extends django. contrib. admin. ModelAdmin class.


1 Answers

First you'd need to import and subclass GroupAdmin from django.contrib.auth.admin. In your subclass, define a user_count method. Then, unregister the existing Group model from the admin, 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)
like image 150
Daniel Roseman Avatar answered Oct 06 '22 18:10

Daniel Roseman