Django

Code

root/django/trunk/django/contrib/admin/__init__.py

Revision 8583, 1.7 kB (checked in by jacob, 3 months ago)

Fixed #8126: improved admin.autodiscover() to be more forgiving of invalid INSTALLED_APPS entries and/or exotic import schemes that don't have __path__.

  • Property svn:eol-style set to native
Line 
1 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
2 from django.contrib.admin.options import StackedInline, TabularInline
3 from django.contrib.admin.sites import AdminSite, site
4
5 def autodiscover():
6     """
7     Auto-discover INSTALLED_APPS admin.py modules and fail silently when
8     not present. This forces an import on them to register any admin bits they
9     may want.
10     """
11     import imp
12     from django.conf import settings
13
14     for app in settings.INSTALLED_APPS:
15         # For each app, we need to look for an admin.py inside that app's
16         # package. We can't use os.path here -- recall that modules may be
17         # imported different ways (think zip files) -- so we need to get
18         # the app's __path__ and look for admin.py on that path.
19
20         # Step 1: find out the app's __path__ Import errors here will (and
21         # should) bubble up, but a missing __path__ (which is legal, but weird)
22         # fails silently -- apps that do weird things with __path__ might
23         # need to roll their own admin registration.
24         try:
25             app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__
26         except AttributeError:
27             continue
28
29         # Step 2: use imp.find_module to find the app's admin.py. For some
30         # reason imp.find_module raises ImportError if the app can't be found
31         # but doesn't actually try to import the module. So skip this app if
32         # its admin.py doesn't exist
33         try:
34             imp.find_module('admin', app_path)
35         except ImportError:
36             continue
37
38         # Step 3: import the app's admin file. If this has errors we want them
39         # to bubble up.
40         __import__("%s.admin" % app)
Note: See TracBrowser for help on using the browser.