django 用户和组 - 在管理员中显示组成员身份? [英] django users and groups - show group membership in admin?

查看:19
本文介绍了django 用户和组 - 在管理员中显示组成员身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django 管理员的用户管理使我们可以通过选择组的列表轻松访问设置用户的组忠诚度.这是分配的多对多关系默认小部件.在大多数情况下,这对于从一开始就创建用户很有好处.

The django admin's User management gives us easy access to setting a User's group allegiances via the list for picking the Groups. This is the ManyToMany relationship default widget for assignment. And for the most part that's nice for User creation from the get go.

但是,假设我有大量用户和群组...我希望有机会通过单击管理员中的群组来查看和管理群组成员资格,并在类似的选择器中查看所有成员(或通过水平小部件).

However, say I have a huge number of users and groups...I'd like the chance to view and manage group membership by clicking on the Group in the admin and see all the members in a similar picker (or via the horizontal widget).

是否有内置的 django 方法/技巧以反向"显示多对多关系?

Is there a builtin django method/trick to show ManyToMany relations in "reverse?"

在用户创建的用户管理表单中设置组分配很好.但是,一旦用户和组建立良好,查找和管理这可能会很痛苦.

Setting group assignment is fine from the User adminform on User creation. But this is can be big pain to find and manage once users and groups are well established.

我想解决方法是让我自己的视图显示组成员,我只是想看看是否有一些技巧可以在管理中使用内联或其他东西.

I guess the workaround is to make my own view to show group members, I just wanted to see if there was some trick I could do with inlines or something in the admin.

推荐答案

尝试使用这个snippet.这是 contrib.auth 应用管理部分的自定义示例.

Try to use this snippet. It's customisation example of contrib.auth app admin part.

# utils/admin_auth.py
# -*- coding: utf-8 -*-
from django.utils.safestring import mark_safe
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
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([u"<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 = u'Groups'

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'

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'

from django.core.urlresolvers import reverse
def persons(self):
    return ', '.join(['<a href="%s">%s</a>' % (reverse('admin:auth_user_change', args=(x.id,)), x.username) for x in self.user_set.all().order_by('username')])
persons.allow_tags = True

class UserAdmin(UserAdmin):
    list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff, adm, roles, last]
    list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']

class GroupAdmin(GroupAdmin):
    list_display = ['name', persons]
    list_display_links = ['name']

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, UserAdmin)
admin.site.register(Group, GroupAdmin)

这篇关于django 用户和组 - 在管理员中显示组成员身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆