5

In my new mezzanine site I want to remove some models from the admin menu. So I defined ADMIN_MENU_ORDER with the models I want to see in the admin menu. This results in the other models, that are not in the list (BlogPost, ThreadedComment, Site, Redirect), being listed below my desired models.

So I used this to remove them:

ADMIN_REMOVAL = ('blog.BlogPost',
                 'generic.ThreadedComment',
                 'sites.Site',
                 'redirects.Redirect',)

(I simply cut and pasted from the default ADMIN_MENU_ORDER.) But this had no effect because of an ImportError in mezzanine.urls (line 19-31):

# Remove unwanted models from the admin that are installed by default with
# third-party apps.
for model in settings.ADMIN_REMOVAL:
    try:
        model = tuple(model.rsplit(".", 1))
        exec("from %s import %s" % model)
    except ImportError:
        pass
    else:
        try:
            admin.site.unregister(eval(model[1]))
        except NotRegistered:
            pass

So I changed ADMIN_REMOVAL like this:

ADMIN_REMOVAL = ('blog.models.BlogPost',
                 'generic.models.ThreadedComment',
                 'django.contrib.sites.models.Site',
                 'django.contrib.redirects.models.Redirect',)

But this still has no effect although there is no ImportError or NotRegistered in this code lines.

There seems to be something, that I still have not understood.

Update: I did some debugging. The line

            admin.site.unregister(eval(model[1]))

from mezzanine/urls.py (cited above) actually calls mezzanine.boot.lazy_admin.unregister, which looks like this:

def unregister(self, *args, **kwargs):
    self._deferred.append(("unregister", args, kwargs))

It looks like after the boot process mezzanine.boot.lazy_admin.lazy_registration should be called, to apply all the deferred registering and unregistering:

def lazy_registration(self):
    for name, deferred_args, deferred_kwargs in self._deferred:
        getattr(AdminSite, name)(self, *deferred_args, **deferred_kwargs)

But actually mezzanine.boot.lazy_admin.lazy_registration is called before the code in mezzanine/urls.py, that tries to unregister the models from ADMIN_REMOVAL.

(I added print statements like this and got the following output:)

mezzanine/urls.py

for model in settings.ADMIN_REMOVAL:
    [...]
        try:
            print 'unregistering', model[1]
            admin.site.unregister(eval(model[1]))
        except NotRegistered:
            pass

mezzanine/boot/lazy_admin.py

def lazy_registration(self):
    print 'lazy_registration occurs'
    for name, deferred_args, deferred_kwargs in self._deferred:
        getattr(AdminSite, name)(self, *deferred_args, **deferred_kwargs)

Output

lazy_registration occurs
unregistering BlogPost
unregistering ThreadedComment
unregistering Site
unregistering Redirect

I will post this as an issue on Github

jammon
  • 3,404
  • 3
  • 20
  • 29

0 Answers0