我的代码曾经在Django 1.3中工作,但在更新到Django 1.4之后它已不再存在了。 我们的想法是为django-admin-tools构建一个MenuItem,其中包含一个来自应用程序的模型列表。
from admin_tools.utils import AppListElementMixin
from app import models as my_models
class CustomMenu(Menu):
def init_with_context(self, context):
app_list=AppListElementMixin()
'''ERROR not working after upgrade to django 1.4, returns empty list'''
all_models = get_models(app_mod=my_models)
''''''
dict_models = {}
for model in all_models:
dict_models[model.__name__] = items.MenuItem(
title=model._meta.verbose_name_plural,
url=app_list._get_admin_change_url(model, context)
)
答案 0 :(得分:2)
尝试将您的'app'添加到settings.py INSTALLED_APPS中 然后这样做
from django.db.models import get_models, get_app
和这个
all_models = get_models(app_mod=get_app('app'))
答案 1 :(得分:0)
如果您的目标是为每个模型添加一个菜单项,您可以尝试:
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from admin_tools.menu import items, Menu
class CustomMenu(Menu):
def __init__(self, **kwargs):
Menu.__init__(self, **kwargs)
self.children += [
# Other items that are in the menu eg "items.Bookmarks()," go here
items.AppList(
_('Name of the submenu'), # Your own choosing
models=('app.models.*',) # Assuming your django app is called "app"
)
]
def init_with_context(self, context):
return super(CustomMenu, self).init_with_context(context)
这就是我在我的一个项目中所做的工作。