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 4 years, 9 months ago
  2. Improved User Admin by gregb 3 years, 11 months ago
  3. Generic csv export admin action by dek 4 years, 8 months ago
  4. FieldsetForm by Ciantic 7 years ago
  5. Group permissions selection in admin filtered by your app models by estecb 2 months, 2 weeks 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 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?)