Browse files

newforms-admin: Added autodiscover functionality to…

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

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 84541cd commit 2e72445fce44d95914921448af846ff7a2f70167 @brosner brosner committed Jul 9, 2008
Showing with 22 additions and 0 deletions.
  1. +13 −0 django/contrib/admin/
  2. +9 −0 docs/admin.txt
@@ -1,3 +1,16 @@
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site
+def autodiscover():
+ """
+ Auto-discover INSTALLED_APPS modules and fail silently when
+ not present. This forces an import on them to register any admin bits they
+ may want.
+ """
+ from django.conf import settings
+ for app in settings.INSTALLED_APPS:
+ try:
+ __import__("%s.admin" % app)
+ except ImportError:
+ pass
@@ -609,11 +609,16 @@ In this example, we register the default ``AdminSite`` instance
from django.conf.urls.defaults import *
from django.contrib import admin
+ admin.autodiscover()
urlpatterns = patterns('',
+Above we used ``admin.autodiscover()`` to automatically load the
+``INSTALLED_APPS`` modules.
In this example, we register the ``AdminSite`` instance
``myproject.admin.admin_site`` at the URL ``/myadmin/`` ::
@@ -625,6 +630,10 @@ In this example, we register the ``AdminSite`` instance
('^myadmin/(.*)', admin_site.root),
+There is really no need to use autodiscover when using your own ``AdminSite``
+instance since you will likely be importing all the per-app modules
+in your ``myproject.admin`` module.
Note that the regular expression in the URLpattern *must* group everything in
the URL that comes after the URL root -- hence the ``(.*)`` in these examples.

0 comments on commit 2e72445

Please sign in to comment.