Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9427: Allow for autodiscover to load admin modules from apps i…

…n eggs. Thanks clint and metzen.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12989 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a213599db7781c8945f161fd77e5c4b706b99853 1 parent a288f97
Karen Tracey authored

Showing 1 changed file with 9 additions and 28 deletions. Show diff stats Hide diff stats

  1. 37  django/contrib/admin/__init__.py
37  django/contrib/admin/__init__.py
@@ -2,7 +2,6 @@
2 2
 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
3 3
 from django.contrib.admin.options import StackedInline, TabularInline
4 4
 from django.contrib.admin.sites import AdminSite, site
5  
-from django.utils.importlib import import_module
6 5
 
7 6
 
8 7
 def autodiscover():
@@ -13,36 +12,13 @@ def autodiscover():
13 12
     """
14 13
 
15 14
     import copy
16  
-    import imp
17 15
     from django.conf import settings
  16
+    from django.utils.importlib import import_module
  17
+    from django.utils.module_loading import module_has_submodule
18 18
 
19 19
     for app in settings.INSTALLED_APPS:
20  
-        # For each app, we need to look for an admin.py inside that app's
21  
-        # package. We can't use os.path here -- recall that modules may be
22  
-        # imported different ways (think zip files) -- so we need to get
23  
-        # the app's __path__ and look for admin.py on that path.
24  
-
25  
-        # Step 1: find out the app's __path__ Import errors here will (and
26  
-        # should) bubble up, but a missing __path__ (which is legal, but weird)
27  
-        # fails silently -- apps that do weird things with __path__ might
28  
-        # need to roll their own admin registration.
29 20
         mod = import_module(app)
30  
-        try:
31  
-            app_path = mod.__path__
32  
-        except AttributeError:
33  
-            continue
34  
-
35  
-        # Step 2: use imp.find_module to find the app's admin.py. For some
36  
-        # reason imp.find_module raises ImportError if the app can't be found
37  
-        # but doesn't actually try to import the module. So skip this app if
38  
-        # its admin.py doesn't exist
39  
-        try:
40  
-            imp.find_module('admin', app_path)
41  
-        except ImportError:
42  
-            continue
43  
-
44  
-        # Step 3: import the app's admin file. If this has errors we want them
45  
-        # to bubble up.
  21
+        # Attempt to import the app's admin module.
46 22
         try:
47 23
             before_import_registry = copy.copy(site._registry)
48 24
             import_module('%s.admin' % app)
@@ -52,4 +28,9 @@ def autodiscover():
52 28
             # could raise NotRegistered and AlreadyRegistered exceptions
53 29
             # (see #8245).
54 30
             site._registry = before_import_registry
55  
-            raise
  31
+
  32
+            # Decide whether to bubble up this error. If the app just
  33
+            # doesn't have an admin module, we can ignore the error
  34
+            # attempting to import it, otherwise we want it to bubble up.
  35
+            if module_has_submodule(mod, 'admin'):
  36
+                raise

0 notes on commit a213599

Please sign in to comment.
Something went wrong with that request. Please try again.