[Fixed]-Why isn't admin.autodiscover() called automatically in Django when using the admin, why was it designed to be called explicitly?

15👍

(edit: Obsoleted after Django 1.7+, not necessary more, see Alasdair’s answer)

I think it’s about giving you finer control. Consider the code of contrib.admin.autodiscover:

def autodiscover():
    """
    Auto-discover INSTALLED_APPS admin.py modules and fail silently when
    not present. This forces an import on them to register any admin bits they
    may want.
    """

    import copy
    from django.conf import settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's admin module.
        try:
            before_import_registry = copy.copy(site._registry)
            import_module('%s.admin' % app)
        except:
            # Reset the model registry to the state before the last import as
            # this import will have to reoccur on the next request and this
            # could raise NotRegistered and AlreadyRegistered exceptions
            # (see #8245).
            site._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an admin module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'admin'):
                raise

So it will automatically load the INSTALLED_APPS admin.py modules and fail silently when not found. Now, there are cases when you actually don’t want that such as when using your own AdminSite:

# urls.py
from django.conf.urls import patterns, url, include
from myproject.admin import admin_site

urlpatterns = patterns('',
    (r'^myadmin/', include(admin_site.urls)),
)

in this case, you don’t need to call autodiscovery().

There are also other times when you only want to see or edit a subset of apps of your projects through admin, and calling autodiscovery() would not enable you to do that.

👤K Z

19👍

Before Django 1.7, the recommendation was to put the admin.autodiscover() call in urls.py. That allowed it to be disabled if necessary. Requiring admin.autodiscover() instead of calling it automatically was an example of the Python philosophy ‘Explicit is better than implicit’ in action. Remember, the django.contrib.admin app is optional, it is not installed on every site, so it wouldn’t make sense to always run autodiscover.

Most of the time autodiscover works well enough. However if you require more control, you can manually import specific apps’ admin files instead. For example, you might want to register multiple admin sites with different apps in each.

App loading was refactored in Django 1.7. The autodiscover() was moved to the admin app’s default app config. That means that autodiscover now runs when the admin app is loaded, and there’s no need to add admin.autodiscover() to your urls.py. If you do not want autodiscovery, you can now disable it by using the SimpleAdminConfig instead.

4👍

Django doesn’t require you to use django.contrib.admin on every site – it’s not a core module.

Leave a comment