summaryrefslogtreecommitdiff
path: root/django/contrib/admin/__init__.py
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-08-26 16:18:20 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-08-26 16:18:20 +0000
commita516ebdff1b33aacf6c1a9c9773906a5f6570e43 (patch)
tree62ec60d793e5f79c7bb05268c8bca0131a96bb5e /django/contrib/admin/__init__.py
parent30de7331d4f5be03116c681609bf2f7f5ee2354e (diff)
Fixed #8126: improved `admin.autodiscover()` to be more forgiving of invalid `INSTALLED_APPS` entries and/or exotic import schemes that don't have `__path__`.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8583 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/contrib/admin/__init__.py')
-rw-r--r--django/contrib/admin/__init__.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/django/contrib/admin/__init__.py b/django/contrib/admin/__init__.py
index 704dc58ee4..8396ffc6b3 100644
--- a/django/contrib/admin/__init__.py
+++ b/django/contrib/admin/__init__.py
@@ -10,10 +10,31 @@ def autodiscover():
"""
import imp
from django.conf import settings
+
for app in settings.INSTALLED_APPS:
+ # For each app, we need to look for an admin.py inside that app's
+ # package. We can't use os.path here -- recall that modules may be
+ # 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__(app, {}, {}, [app.split('.')[-1]]).__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", __import__(app, {}, {}, [app.split(".")[-1]]).__path__)
+ imp.find_module('admin', app_path)
except ImportError:
- # there is no app admin.py, skip it
continue
+
+ # Step 3: import the app's admin file. If this has errors we want them
+ # to bubble up.
__import__("%s.admin" % app)