More information about users and groups in user admin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#save as useradmin.py
from django.utils.safestring import mark_safe
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.contrib import admin

def roles(self):
    #short_name = unicode # function to get group name
    short_name = lambda x:unicode(x)[:1].upper() # first letter of a group
    p = sorted(["<a title='%s'>%s</a>" % (x, short_name(x)) for x in self.groups.all()])
    if self.user_permissions.count(): p += ['+']
    value = ', '.join(p)
    return mark_safe("<nobr>%s</nobr>" % value)
roles.allow_tags = True
roles.short_description = 'roles'

def last(self):
    fmt = "%b %d, %H:%M"
    #fmt = "%Y %b %d, %H:%M:%S"
    value = self.last_login.strftime(fmt)
    return mark_safe("<nobr>%s</nobr>" % value)
last.allow_tags = True
last.admin_order_field = 'last_login'
last.short_description = 'last login'

def adm(self):
    return self.is_superuser
adm.boolean = True
adm.admin_order_field = 'is_superuser'

def staff(self):
    return self.is_staff
staff.boolean = True
staff.admin_order_field = 'is_staff'

User.roles = roles
User.last = last
User.adm = adm
User.staff = staff

UserAdmin = admin.site._registry[User]
UserAdmin.list_display = ['username', 'email', 'first_name', 'last_name'] \
                       + ['is_active', 'staff', 'adm', 'roles', 'last']

#use last few lines only when you have not very much users in each group
def persons(self):
    return ", ".join([x.username for x in self.user_set.all()])

Group.persons = persons

GroupAdmin = admin.site._registry[Group]
GroupAdmin.list_display = ['name', 'persons']

More like this

  1. More information about users and groups in user admin by ramusus 3 years, 10 months ago
  2. FieldLevelPermissionsAdmin by buriy 5 years, 9 months ago
  3. Admin definition converter (for newforms-admin) by willhardy 4 years, 11 months ago
  4. FieldsetForm by Ciantic 6 years, 2 months ago
  5. Overriding Third-party Admin by mattdw 4 years, 9 months ago

Comments

dvdtho (on July 20, 2008):

The import useradmin must come after admin.autodiscover() is called in your main urls.py in r8010.

#

arcanosam (on January 4, 2012):

when I upgrade to django 1.2 (sometime ago, sorry post this only now...)

I have to fix this:

line 53: GroupAdmin.list_display = ['name', 'persons'] -> GroupAdmin.list_display = ['action_checkbox','__str__', 'persons']

and today I complement with this snippet (at the EOF):

  • http://djangosnippets.org/snippets/2452/

  • include imports was missing and adding one more line after all:

  • GroupAdmin.form = GroupAdminForm

thank you both, owners of this snippets

#

arcanosam (on April 4, 2012):

on django 1.4 I need to move this snippet from root project to the first app that have an admin.py.

I found that import this snippet on urls.py (on root project) gives an error because User and Group models are not yet registered by admin object. It's what I assume or seems.

That's why I try to move it for my first app that has an admin.py and works perfectly again.

see ay

#

(Forgotten your password?)