diff --git a/django/contrib/admin/__init__.py b/django/contrib/admin/__init__.py
a
|
b
|
|
4 | 4 | from django.contrib.admin.sites import AdminSite, site |
5 | 5 | from django.utils.importlib import import_module |
6 | 6 | |
7 | | # A flag to tell us if autodiscover is running. autodiscover will set this to |
8 | | # True while running, and False when it finishes. |
9 | | LOADING = False |
10 | | |
11 | 7 | def autodiscover(): |
12 | 8 | """ |
13 | 9 | Auto-discover INSTALLED_APPS admin.py modules and fail silently when |
14 | 10 | not present. This forces an import on them to register any admin bits they |
15 | 11 | may want. |
16 | 12 | """ |
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). |
22 | | global LOADING |
23 | | if LOADING: |
24 | | return |
25 | | LOADING = True |
26 | | |
27 | 13 | import imp |
| 14 | import copy |
28 | 15 | from django.conf import settings |
29 | 16 | |
30 | 17 | for app in settings.INSTALLED_APPS: |
… |
… |
|
54 | 41 | |
55 | 42 | # Step 3: import the app's admin file. If this has errors we want them |
56 | 43 | # to bubble up. |
57 | | import_module("%s.admin" % app) |
58 | | # autodiscover was successful, reset loading flag. |
59 | | LOADING = False |
| 44 | try: |
| 45 | model_registry_before_import = copy.copy(site._registry) |
| 46 | import_module("%s.admin" % app) |
| 47 | except: |
| 48 | # Reset the model registry to the state before the last import as this import will have to reoccur |
| 49 | # on the next request and this could raise NotRegistered and AlreadyRegistered exceptions (see #8245). |
| 50 | site._registry = model_registry_before_import |
| 51 | raise |
diff --git a/tests/regressiontests/bug8245/tests.py b/tests/regressiontests/bug8245/tests.py
a
|
b
|
|
18 | 18 | else: |
19 | 19 | self.fail( |
20 | 20 | 'autodiscover should have raised a "Bad admin module" error.') |
21 | | # Calling autodiscover again should bail out early and not raise an |
22 | | # AlreadyRegistered error. |
23 | | admin.autodiscover() |
| 21 | # Calling autodiscover again should raise the very same error it did the first time, not an AlreadyRegistered error. |
| 22 | try: |
| 23 | admin.autodiscover() |
| 24 | except Exception, e: |
| 25 | self.failUnlessEqual(str(e), "Bad admin module") |
| 26 | else: |
| 27 | self.fail( |
| 28 | 'autodiscover should have raised a "Bad admin module" error.') |