(edit: Obsoleted después de Django 1.7+, no es necesario más, véase la respuesta de Alasdair)
Creo que es acerca de darle un control más preciso. Considere el código de 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
Por lo tanto, se cargará automáticamente los módulos admin.py INSTALLED_APPS y dejar en silencio cuando no se encuentra. Ahora, hay casos en los que realmente no desea que tal como cuando se utiliza su propio 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)),
)
en este caso, no es necesario llamar a autodiscovery()
.
También hay otras ocasiones en las que solo desea ver o editar un subconjunto de aplicaciones de sus proyectos a través de admin, y llamar al autodiscovery()
no le permitiría hacer eso.
No entiendo por qué necesita cargar aplicaciones. ¿No se cargan las aplicaciones en 'INSTALLED_APPS' cada vez que se ejecuta' settings.py' (por ejemplo, al iniciar el servidor)? – Bentley4
'autodiscover' no está cargando las aplicaciones, está importando' admin.py' para cada aplicación. – Alasdair
@ Bentley4 sí, como dijo Alasdair, solo está importando el 'admin.py' de cada aplicación. –