1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

[1.1.X] Fixed #9427: Allow for autodiscover to load admin modules from apps in eggs. Thanks clint and metzen.

r12989 (and r12192 as a side-effect) from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12990 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2010-04-15 23:18:33 +00:00
parent 231e452f96
commit 534dc44dba

View File

@ -2,7 +2,6 @@ from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
from django.contrib.admin.options import StackedInline, TabularInline from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site from django.contrib.admin.sites import AdminSite, site
from django.utils.importlib import import_module
def autodiscover(): def autodiscover():
@ -13,35 +12,13 @@ def autodiscover():
""" """
import copy import copy
import imp
from django.conf import settings 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: for app in settings.INSTALLED_APPS:
# For each app, we need to look for an admin.py inside that app's mod = import_module(app)
# package. We can't use os.path here -- recall that modules may be # Attempt to import the app's admin module.
# imported different ways (think zip files) -- so we need to get
# the app's __path__ and look for admin.py on that path.
# Step 1: find out the app's __path__ Import errors here will (and
# should) bubble up, but a missing __path__ (which is legal, but weird)
# fails silently -- apps that do weird things with __path__ might
# need to roll their own admin registration.
try:
app_path = import_module(app).__path__
except AttributeError:
continue
# Step 2: use imp.find_module to find the app's admin.py. For some
# reason imp.find_module raises ImportError if the app can't be found
# but doesn't actually try to import the module. So skip this app if
# its admin.py doesn't exist
try:
imp.find_module('admin', app_path)
except ImportError:
continue
# Step 3: import the app's admin file. If this has errors we want them
# to bubble up.
try: try:
before_import_registry = copy.copy(site._registry) before_import_registry = copy.copy(site._registry)
import_module('%s.admin' % app) import_module('%s.admin' % app)
@ -51,4 +28,9 @@ def autodiscover():
# could raise NotRegistered and AlreadyRegistered exceptions # could raise NotRegistered and AlreadyRegistered exceptions
# (see #8245). # (see #8245).
site._registry = before_import_registry 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 raise