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
7
# A flag to tell us if autodiscover is running. autodiscover will set this to
8
# True while running, and False when it finishes.
14
10
not present. This forces an import on them to register any admin bits they
17
# Bail out if autodiscover didn't finish loading from a previous call so
18
# that we avoid running autodiscover again when the URLconf is loaded by
19
# the exception handler to resolve the handler500 view. This prevents an
20
# admin.py module with errors from re-registering models and raising a
21
# spurious AlreadyRegistered exception (see #8245).
28
15
from django.conf import settings
16
from django.utils.importlib import import_module
17
from django.utils.module_loading import module_has_submodule
30
19
for app in settings.INSTALLED_APPS:
31
# For each app, we need to look for an admin.py inside that app's
32
# package. We can't use os.path here -- recall that modules may be
33
# imported different ways (think zip files) -- so we need to get
34
# the app's __path__ and look for admin.py on that path.
36
# Step 1: find out the app's __path__ Import errors here will (and
37
# should) bubble up, but a missing __path__ (which is legal, but weird)
38
# fails silently -- apps that do weird things with __path__ might
39
# need to roll their own admin registration.
41
app_path = import_module(app).__path__
42
except AttributeError:
45
# Step 2: use imp.find_module to find the app's admin.py. For some
46
# reason imp.find_module raises ImportError if the app can't be found
47
# but doesn't actually try to import the module. So skip this app if
48
# its admin.py doesn't exist
50
imp.find_module('admin', app_path)
54
# Step 3: import the app's admin file. If this has errors we want them
56
import_module("%s.admin" % app)
57
# autodiscover was successful, reset loading flag.
20
mod = import_module(app)
21
# Attempt to import the app's admin module.
23
before_import_registry = copy.copy(site._registry)
24
import_module('%s.admin' % app)
26
# Reset the model registry to the state before the last import as
27
# this import will have to reoccur on the next request and this
28
# could raise NotRegistered and AlreadyRegistered exceptions
30
site._registry = before_import_registry
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'):