Django admin - 更改权限列表

发布于 2024-11-18 15:11:53 字数 73 浏览 2 评论 0原文

是否可以更改用户编辑页面中的权限列表?我不想显示所有权限,例如管理日志条目或身份验证组等。 如何修改主查询集以排除其中的某些查询集?

Is there any possibility to change permissions list in user edit page? I don't wan't to show all of permissions for example admin log entry or auth group etc.
How can I modify a main queryset to exclude some of it?

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

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

发布评论

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

评论(2

江城子 2024-11-25 15:11:53

我从此主题中得到了这个想法,它也回答了您的问题,但还不是那么清楚。

您必须覆盖用于可视化的 UserAdmin 表单中的用户权限查询集。

为此,最简单的方法是创建 UserAdmin 的子类并覆盖 get_form 方法:

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

class MyUserAdmin(UserAdmin):
    def get_form(self, request, obj=None, **kwargs):
        # Get form from original UserAdmin.
        form = super(MyUserAdmin, self).get_form(request, obj, **kwargs)
        if 'user_permissions' in form.base_fields:
            permissions = form.base_fields['user_permissions']
            permissions.queryset = permissions.queryset.filter(content_type__name='log entry')
        return form

您可以根据需要更改查询集的过滤器:
示例:

# Exclude admin and auth.    
permissions.queryset = permissions.queryset.exclude(content_type__app_label__in=['admin', 'auth'])

# Only view permissions of desired models (Can be your models or Django's)
permissions.queryset = permissions.queryset.filter(content_type__model__in=['blog', 'post', 'user', 'group'])

创建课程后,您必须向新创建的管理员注册您的用户模型:

admin.site.unregister(User)  # You must unregister first
admin.site.register(User, MyUserAdmin)

编辑:
我添加了 Maik Hoepfel 的评论,因为这段代码使 django 在创建新用户时崩溃。


您可以对群组编辑页面中的权限列表执行相同的操作,但您必须创建另一个从 GroupAdmin 扩展的管理员,并将 form.base_fields['user_permissions'] 更改为 form。 base_fields['权限']

I got the idea from this topic, which also answer your question, but it's not that clear.

You have to overwrite the queryset of user permissions in the UserAdmin form used for visualization.

To do this, the easiest way is to create a subclass of UserAdmin and overwrite the get_form method:

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

class MyUserAdmin(UserAdmin):
    def get_form(self, request, obj=None, **kwargs):
        # Get form from original UserAdmin.
        form = super(MyUserAdmin, self).get_form(request, obj, **kwargs)
        if 'user_permissions' in form.base_fields:
            permissions = form.base_fields['user_permissions']
            permissions.queryset = permissions.queryset.filter(content_type__name='log entry')
        return form

You can change the filter of your queryset for whatever you want:
Examples:

# Exclude admin and auth.    
permissions.queryset = permissions.queryset.exclude(content_type__app_label__in=['admin', 'auth'])

# Only view permissions of desired models (Can be your models or Django's)
permissions.queryset = permissions.queryset.filter(content_type__model__in=['blog', 'post', 'user', 'group'])

After you create your class, you have to register your User model with your newly created Admin:

admin.site.unregister(User)  # You must unregister first
admin.site.register(User, MyUserAdmin)

Edit:
I added comment from Maik Hoepfel, because this code made django crashed when creating new user.


You can do the same with the permission list in your Group edit page, but you have to create another Admin that extends from GroupAdmin, and change form.base_fields['user_permissions'] with form.base_fields['permissions']

夜光 2024-11-25 15:11:53

雷纳托的回答几乎是完美的。 Django 管理员使用相同的形式添加用户是一个两步过程,他的代码在第一步失败,并出现“user_permissions”的 KeyError。

修复很简单,只需使用下面的代码即可:

def get_form(self, request, obj=None, **kwargs):
    form = super(MyUserAdmin, self).get_form(request, obj, **kwargs)
    # adding a User via the Admin doesn't include the permissions at first
    if 'user_permissions' in form.base_fields:
        permissions = form.base_fields['user_permissions']
        permissions.queryset = permissions.queryset.filter(content_type__name='log entry')
    return form

Renato's answer is almost perfect. The Django Admin makes adding a user a two-step process with the same form, and his code fails with a KeyError for 'user_permissions' in the first step.

The fix is easy enough, just use the code below instead:

def get_form(self, request, obj=None, **kwargs):
    form = super(MyUserAdmin, self).get_form(request, obj, **kwargs)
    # adding a User via the Admin doesn't include the permissions at first
    if 'user_permissions' in form.base_fields:
        permissions = form.base_fields['user_permissions']
        permissions.queryset = permissions.queryset.filter(content_type__name='log entry')
    return form
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文