在Django管理中,如何按组过滤用户?

发布于 2024-07-21 03:03:36 字数 34 浏览 5 评论 0原文

它可以让您按员工状态和超级用户状态进行过滤,但是组呢?

It gives you filter by staff status and superuser status, but what about groups?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伊面 2024-07-28 03:03:36

从版本 1.3 开始,可以使用 使用this

list_filter = ('groups__name')

当然,正如 @S.Lott 所解释的,您必须在 admin.py 文件中注册您的自定义类:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + ('groups__name',)

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

Since version 1.3 it can be done using this:

list_filter = ('groups__name')

Of course as @S.Lott explains you must register your customized class in the admin.py file:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + ('groups__name',)

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
拧巴小姐 2024-07-28 03:03:36

请参阅在 Django 中自定义管理表单,同时还使用自动发现< /a>

本质上,您可以使用您想要的功能定义自定义的 Admin 类。

然后注销并注册您修改后的 Admin 类。

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

See Customizing an Admin form in Django while also using autodiscover

Essentially, you define a customized Admin class with the features you want.

Then unregister and register your revised Admin class.

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
无边思念无边月 2024-07-28 03:03:36

下面是一个完整的示例,它继承自 SimpleListFilter,它在 Django 1.4 及更高版本中可用(截至 2023 年 5 月):

https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

它支持设置所有可用的标签和参数来创建完全自定义的过滤器。

它在过滤器面板中显示为“按组”,并包含所有可用组的列表。

from django.contrib.admin import SimpleListFilter
from django.contrib.auth.models import Group
from django.utils.translation import ugettext as _

class GroupListFilter(SimpleListFilter):
    title = _('group')
    parameter_name = 'group'

    def lookups(self, request, model_admin):
        items = ()
        for group in Group.objects.all():
            items += ((str(group.id), str(group.name),),)
        return items

    def queryset(self, request, queryset):
        group_id = request.GET.get(self.parameter_name, None)
        if group_id:
            return queryset.filter(groups=group_id)
        return queryset

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + (GroupListFilter,)

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

Here is a complete example, that inherits from SimpleListFilter, which is available in Django 1.4 and up (current as of May 2023):

https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

It support setting all available labels and parameters to create the completely custom filter.

It shows up as "By group" in the filter panel, with a list of all available groups.

from django.contrib.admin import SimpleListFilter
from django.contrib.auth.models import Group
from django.utils.translation import ugettext as _

class GroupListFilter(SimpleListFilter):
    title = _('group')
    parameter_name = 'group'

    def lookups(self, request, model_admin):
        items = ()
        for group in Group.objects.all():
            items += ((str(group.id), str(group.name),),)
        return items

    def queryset(self, request, queryset):
        group_id = request.GET.get(self.parameter_name, None)
        if group_id:
            return queryset.filter(groups=group_id)
        return queryset

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + (GroupListFilter,)

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
渔村楼浪 2024-07-28 03:03:36

在 Django 的更高版本中,它的工作方式与您所期望的完全一样:

list_filter = ('groups', )

无需取消注册/注册管理类。

In later versions of Django, it works exactly as you'd expect:

list_filter = ('groups', )

No need to unregister/register the admin class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文