django: displaying group users count in admin
I would like to change admin for a group, so it would display how many 开发者_JAVA技巧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
.
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)
精彩评论