在使用 Django 结合 django-admin-tools 时,我遇到了一个问题。在对 SiteAdmin 进行子类化并使用该子类注册我的 admin 类之后,Applications 的整个菜单都消失了。我对 index 和 app_index 的修改只是将 admin_model 的图片属性添加到 model_dict 中。
import six
from django.contrib import admin
from django.contrib.auth.models import User, Group
from django.contrib.sites.models import Site
class CustomAdminSite(AdminSite):
@never_cache
def index(self, request, extra_context=None):
"""
Displays the main admin index page, which lists all of the installed
apps that have been registered in this site.
"""
app_dict = {}
user = request.user
for model, model_admin in self._registry.items():
app_label = model._meta.app_label
has_module_perms = user.has_module_perms(app_label)
if has_module_perms:
perms = model_admin.get_model_perms(request)
# Check whether user has any perm for this module.
# If so, add the module to the model_list.
if True in perms.values():
info = (app_label, model._meta.module_name)
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'perms': perms,
}
if hasattr(model_admin, 'picture'):model_dict['picture'] = model_admin.picture
if perms.get('change', False):
try:
model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=self.name)
except NoReverseMatch:
pass
if perms.get('add', False):
try:
model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=self.name)
except NoReverseMatch:
pass
if app_label in app_dict:
app_dict[app_label]['models'].append(model_dict)
else:
app_dict[app_label] = {
'name': app_label.title(),
'app_url': reverse('admin:app_list', kwargs={'app_label': app_label}, current_app=self.name),
'has_module_perms': has_module_perms,
'models': [model_dict],
}
# Sort the apps alphabetically.
app_list = list(six.itervalues(app_dict))
app_list.sort(key=lambda x: x['name'])
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: x['name'])
context = {
'title': _('Site administration'),
'app_list': app_list,
}
context.update(extra_context or {})
return TemplateResponse(request, self.index_template or
'admin/index.html', context,
current_app=self.name)
def app_index(self, request, app_label, extra_context=None):
user = request.user
has_module_perms = user.has_module_perms(app_label)
app_dict = {}
for model, model_admin in self._registry.items():
if app_label == model._meta.app_label:
if has_module_perms:
perms = model_admin.get_model_perms(request)
# Check whether user has any perm for this module.
# If so, add the module to the model_list.
if True in perms.values():
info = (app_label, model._meta.module_name)
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'perms': perms,
}
if hasattr(model_admin, 'picture'):model_dict['picture'] = model_admin.picture
if perms.get('change', False):
try:
model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=self.name)
except NoReverseMatch:
pass
if perms.get('add', False):
try:
model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=self.name)
except NoReverseMatch:
pass
if app_dict:
app_dict['models'].append(model_dict),
else:
# First time around, now that we know there's
# something to display, add in the necessary meta
# information.
app_dict = {
'name': app_label.title(),
'app_url': '',
'has_module_perms': has_module_perms,
'models': [model_dict],
}
if not app_dict:
raise Http404('The requested admin page does not exist.')
# Sort the models alphabetically within each app.
app_dict['models'].sort(key=lambda x: x['name'])
context = {
'title': _('%s administration') % capfirst(app_label),
'app_list': [app_dict],
}
context.update(extra_context or {})
return TemplateResponse(request, self.app_index_template or [
'admin/%s/app_index.html' % app_label,
'admin/app_index.html'
], context, current_app=self.name)
custom_site = CustomAdminSite(name='admin')
custom_site.register(Group, GroupAdmin)
custom_site.register(User, UserAdmin)
custom_site.register(Site, SiteAdmin)
我以相同的方式注册了我的所有应用程序模型,显示了身份验证和站点菜单,但我的应用程序和它们的 model_admin 都没有显示。
- 解决方案
根据提供的答案,Django admin-tools 仅在使用自定义管理类时支持自定义仪表板:
Multiple admin site support in django-admin-tools is, at the moment, limited to dashboards.
This means you cannot have different menus or theming for each instance of admin sites. This will change in the near near future though.
See here for more info: Working with multiple admin sites
由上可知,这是 Django admin-tools 的限制,它目前只支持自定义仪表板,而不支持自定义菜单或主题。因此,我只能等待官方更新,才能解决此问题。