How to use ADMIN_REMOVAL on the mezzanine

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 leads to other models that are not included in the list ( BlogPost

, ThreadedComment

, Site

, Redirect

), which are listed below my desired model.

So I used this to remove them:

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

      

(I just cut and paste from the default ADMIN_MENU_ORDER

). But this did not affect 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

as follows:

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

      

But that still doesn't work, even though there is no ImportError

or in those code lines NotRegistered

.

It seems to be something that I still haven't figured out.

Update: I've done some debugging. Line

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

      

from mezzanine/urls.py

(quoted 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 has to be called mezzanine.boot.lazy_admin.lazy_registration

to apply all pending registrations and registrations:

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

      

But it is actually mezzanine.boot.lazy_admin.lazy_registration

called before by the code in mezzanine/urls.py

which tries to unregister the models from ADMIN_REMOVAL

.

(I added print instructions as follows 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

+3


source to share





All Articles