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
.
views:
76answers:
1
+2
A:
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)
Daniel Roseman
2010-03-24 14:14:02
Funny, I already have my own GroupAdmin, just forgot, that I was doing something on that (easier user addition, since you can't add users from default group admin). It was so long time ago, that I simply forgot about that.. Thanks a lot for your help :-)
gruszczy
2010-03-24 14:18:17
Ok, a field appears, but it is always None and user_count function is not called. Any idea, why is this happening?
gruszczy
2010-03-24 14:22:45
Ohk, it should be obj.user_set.count(), rather than self.user_set.count(). Now it works :-)
gruszczy
2010-03-24 14:25:16