Code

Ticket #8500: default_admin_site_override_2.diff

File default_admin_site_override_2.diff, 1.9 KB (added by oyvind, 6 years ago)

another possible solution

Line 
1diff --git a/django/contrib/admin/__init__.py b/django/contrib/admin/__init__.py
2index 704dc58..6f276a6 100644
3--- a/django/contrib/admin/__init__.py
4+++ b/django/contrib/admin/__init__.py
5@@ -1,6 +1,28 @@
6 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
7 from django.contrib.admin.options import StackedInline, TabularInline
8-from django.contrib.admin.sites import AdminSite, site
9+from django.contrib.admin.sites import AdminSite
10+
11+# This global object represents the default admin site, for the common case.
12+# You can instantiate AdminSite in your own code to create a custom admin site.
13+# It is also possible to replace the default site by passing the path to your
14+# own adminsite class to admin.set_default_site before calling admin.autodiscover().
15+
16+site = AdminSite()
17+
18+def set_default_site(import_path):
19+    try:
20+        dot = import_path.rindex('.')
21+    except ValueError:
22+        raise ImproperlyConfigured("%s isn't a adminsite class." % import_path)
23+    module, classname = import_path[:dot], import_path[dot+1:]
24+    try:
25+        mod = __import__(module, {}, {}, [''])
26+    except ImportError, e:
27+        raise ImproperlyConfigured('Error importing module %s: "%s"' % (module, e))
28+    try:
29+        site = getattr(mod, classname)()
30+    except AttributeError:
31+        raise ImproperlyConfigured('The module "%s" does not define a "%s" class.' % (module, classname))
32 
33 def autodiscover():
34     """
35diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
36index 81e43a1..8b8062b 100644
37--- a/django/contrib/admin/sites.py
38+++ b/django/contrib/admin/sites.py
39@@ -402,6 +402,3 @@ class AdminSite(object):
40             'app_list': [app_dict]
41         }, context_instance=template.RequestContext(request))
42 
43-# This global object represents the default admin site, for the common case.
44-# You can instantiate AdminSite in your own code to create a custom admin site.
45-site = AdminSite()