2

My code used to work in Django 1.3, but after the update to Django 1.4 it does not anymore. The idea is to build a MenuItem for django-admin-tools, with a list of models from an app.

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)
                                            )
pedrovgp
  • 767
  • 9
  • 23

2 Answers2

2

Try to add your 'app' into settings.py INSTALLED_APPS then do this

from django.db.models import get_models, get_app

and this

all_models = get_models(app_mod=get_app('app'))
rmnff
  • 386
  • 2
  • 4
0

If your objective is to add a menu item for each model, you can try:

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)

This is what I have working in one of my projects.

Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
  • I used it, but I was trying to group the models under different group names and eliminate the app step in the Menu. What I can't understand is why is get_models returning an empty model after upgrading to Django-1.4 – pedrovgp Oct 10 '12 at 13:36