我正在使用Django 1.8.1。这是我第一次自定义管理员屏幕。
默认情况下,列表中有一些权限,例如admin | entry log,auth | group等。我想从列表中删除这些权限。我可以借助Django admin - change permissions list
从用户编辑屏幕上的列表中筛选出这些权限我的问题是添加/编辑组屏幕。在这个屏幕上,这些许可仍然存在。以下是该页面的屏幕截图。
如何从此列表中删除部分权限?
答案 0 :(得分:4)
我尝试通过扩展GroupAdmin类并覆盖get_form方法来实现此目的。但不幸的是,这对我没有用。根本没有调用get_form方法。我不知道原因。
以下解决了我的问题:
class MyGroupAdminForm(forms.ModelForm):
class Meta:
model = Group
fields = ('name', 'permissions')
permissions = forms.ModelMultipleChoiceField(
Permission.objects.exclude(content_type__app_label__in=['auth','admin','sessions','users','contenttypes']),
widget=admin.widgets.FilteredSelectMultiple(_('permissions'), False))
class MyGroupAdmin(admin.ModelAdmin):
form = MyGroupAdminForm
search_fields = ('name',)
ordering = ('name',)
admin.site.unregister(Group)
admin.site.register(Group, MyGroupAdmin)
通过上面的代码,我成功地从添加/编辑组屏幕上的可用权限列表中删除了应用[auth,admin,sessions,users,contenttypes]的权限。
答案 1 :(得分:1)
@Niraj Chapla使@JRodDynamite的解决方案有效,需要注销并在覆盖后注册该组。
from django.contrib.auth.models import Group
from django.contrib.auth.admin import GroupAdmin
class MyGroupAdmin(GroupAdmin):
def get_form(self, request, obj=None, **kwargs):
# Get form from original GroupAdmin.
form = super(MyGroupAdmin, self).get_form(request, obj, **kwargs)
if 'permissions' in form.base_fields:
permissions = form.base_fields['permissions']
permissions.queryset = permissions.queryset.exclude(content_type__app_label__in=['admin', 'auth']) # Example
return form
############ ADD THIS #############
admin.site.unregister(Group)
admin.site.register(Group, MyGroupAdmin)
###################################
答案 2 :(得分:0)
正如您提供的link中所述,您可以对群组编辑页面中的权限列表执行相同操作,但您必须创建另一个来自GroupAdmin的管理员。
它的代码看起来像这样:
from django.contrib.auth.models import Group
from django.contrib.auth.admin import GroupAdmin
class MyGroupAdmin(GroupAdmin):
def get_form(self, request, obj=None, **kwargs):
# Get form from original GroupAdmin.
form = super(MyGroupAdmin, self).get_form(request, obj, **kwargs)
if 'permissions' in form.base_fields:
permissions = form.base_fields['permissions']
permissions.queryset = permissions.queryset.exclude(content_type__app_label__in=['admin', 'auth']) # Example
return form
答案 3 :(得分:0)
这是Django 1.10.1的最新版本
# coding: utf-8
from django.contrib import admin
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.utils.translation import ugettext_lazy as _
class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups',)}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
class CustomGroupAdmin(GroupAdmin):
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
if db_field.name == 'permissions':
qs = kwargs.get('queryset', db_field.remote_field.model.objects)
qs = qs.exclude(codename__in=(
'add_permission',
'change_permission',
'delete_permission',
'add_contenttype',
'change_contenttype',
'delete_contenttype',
'add_session',
'delete_session',
'change_session',
'add_logentry',
'change_logentry',
'delete_logentry',
))
# Avoid a major performance hit resolving permission names which
# triggers a content_type load:
kwargs['queryset'] = qs.select_related('content_type')
return super(GroupAdmin, self).formfield_for_manytomany(
db_field, request=request, **kwargs)
admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, CustomUserAdmin)
admin.site.register(Group, CustomGroupAdmin)
此代码将删除组中的无用权限并删除用户中的权限字段,如果您不想要用户部分,只需删除相关代码。