Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms-admin: Added autodiscover functionality to django.contrib.ad…

…min. This makes the admin aware of per-app admin.py modules and does an import on them when explicitly called. Docs show how this is used. Fixed #6003, #6776, #6776.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7872 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2e72445fce44d95914921448af846ff7a2f70167 1 parent 84541cd
Brian Rosner authored July 09, 2008
13  django/contrib/admin/__init__.py
... ...
@@ -1,3 +1,16 @@
1 1
 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
2 2
 from django.contrib.admin.options import StackedInline, TabularInline
3 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
+    from django.conf import settings
  12
+    for app in settings.INSTALLED_APPS:
  13
+        try:
  14
+            __import__("%s.admin" % app)
  15
+        except ImportError:
  16
+            pass
9  docs/admin.txt
@@ -609,11 +609,16 @@ In this example, we register the default ``AdminSite`` instance
609 609
     # urls.py
610 610
     from django.conf.urls.defaults import *
611 611
     from django.contrib import admin
  612
+    
  613
+    admin.autodiscover()
612 614
 
613 615
     urlpatterns = patterns('',
614 616
         ('^admin/(.*)', admin.site.root),
615 617
     )
616 618
 
  619
+Above we used ``admin.autodiscover()`` to automatically load the
  620
+``INSTALLED_APPS`` admin.py modules.
  621
+
617 622
 In this example, we register the ``AdminSite`` instance
618 623
 ``myproject.admin.admin_site`` at the URL ``/myadmin/`` ::
619 624
 
@@ -625,6 +630,10 @@ In this example, we register the ``AdminSite`` instance
625 630
         ('^myadmin/(.*)', admin_site.root),
626 631
     )
627 632
 
  633
+There is really no need to use autodiscover when using your own ``AdminSite``
  634
+instance since you will likely be importing all the per-app admin.py modules
  635
+in your ``myproject.admin`` module.
  636
+
628 637
 Note that the regular expression in the URLpattern *must* group everything in
629 638
 the URL that comes after the URL root -- hence the ``(.*)`` in these examples.
630 639
 

0 notes on commit 2e72445

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