我正在使用django-guardian
来管理我的权限。默认情况下,每个模型有3个排列:add
、change
、delete
。如果我添加我自己的权限,例如view
,我将在"用户管理"选项卡中拥有全部4个权限,在"模型对象管理"选项卡中将拥有全部4。
我知道过滤权限是可能的:Django管理员-更改权限列表
但我不知道如何在模型对象管理选项卡中过滤权限。这可能吗?
我做了一些工作。也许这不是最好的解决方案,但有效:
admin_utils.py
:
from __future__ import unicode_literals
from django import forms
from django.conf import settings
from guardian.compat import url, patterns
from django.contrib import admin
from django.contrib import messages
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext, ugettext_lazy as _
from django.db.models import Q
from django.contrib.auth.models import Group, Permission
from guardian.compat import get_user_model
from guardian.forms import UserObjectPermissionsForm
from guardian.forms import GroupObjectPermissionsForm
from guardian.shortcuts import get_perms
from guardian.shortcuts import get_users_with_perms
from guardian.shortcuts import get_groups_with_perms
from guardian.models import Group
from django.contrib.contenttypes.models import ContentType
from guardian.admin import AdminUserObjectPermissionsForm, AdminGroupObjectPermissionsForm
from guardian.admin import GuardedModelAdmin
class MyGuardedModelAdmin(GuardedModelAdmin):
allowed_perms = []
def get_perms_for_model(self, cls):
if isinstance(cls, basestring):
app_label, model_name = cls.split('.')
model = models.get_model(app_label, model_name)
else:
model = cls
ctype = ContentType.objects.get_for_model(model)
return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )
def get_obj_perms_base_context(self, request, obj):
context = {
'adminform': {'model_admin': self},
'media': self.media,
'object': obj,
'app_label': self.model._meta.app_label,
'opts': self.model._meta,
'original': hasattr(obj, '__unicode__') and obj.__unicode__() or
str(obj),
'has_change_permission': self.has_change_permission(request, obj),
'model_perms': self.get_perms_for_model(obj),
'title': _("Object permissions"),
}
return context
class MyAdminUserObjectPermissionsForm(AdminUserObjectPermissionsForm):
def get_perms_for_model(self, cls):
if isinstance(cls, basestring):
app_label, model_name = cls.split('.')
model = models.get_model(app_label, model_name)
else:
model = cls
ctype = ContentType.objects.get_for_model(model)
return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )
def get_obj_perms_field_choices(self):
choices = [(p.codename, p.name) for p in self.get_perms_for_model(self.obj)]
return choices
def get_obj_perms_manage_user_form(self):
setattr(self.MyAdminUserObjectPermissionsForm, 'allowed_perms', self.allowed_perms)
return self.MyAdminUserObjectPermissionsForm
class MyAdminGroupObjectPermissionsForm(GroupObjectPermissionsForm):
def get_perms_for_model(self, cls):
if isinstance(cls, basestring):
app_label, model_name = cls.split('.')
model = models.get_model(app_label, model_name)
else:
model = cls
ctype = ContentType.objects.get_for_model(model)
return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )
def get_obj_perms_field_choices(self):
choices = [(p.codename, p.name) for p in self.get_perms_for_model(self.obj)]
return choices
def get_obj_perms_manage_group_form(self):
setattr(self.MyAdminGroupObjectPermissionsForm, 'allowed_perms', self.allowed_perms)
return self.MyAdminGroupObjectPermissionsForm
现在您可以使用allowed_perms
进行过滤
admin.py
:
class PollAdmin(MyGuardedModelAdmin):
allowed_perms = ['view_poll']
admin.site.register(Poll, PollAdmin)