Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5454: settings.DATABASE_BACKEND may now refer to an external p…

…ackage (i.e. one located outside the Django source. Thanks, George Vilches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6316 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 07447a0f562c62f020f00ba1d5248c5de4c264ca 1 parent 201a6cc
Jacob Kaplan-Moss authored September 15, 2007
55  django/db/__init__.py
... ...
@@ -1,6 +1,9 @@
  1
+import os
1 2
 from django.conf import settings
2 3
 from django.core import signals
  4
+from django.core.exceptions import ImproperlyConfigured
3 5
 from django.dispatch import dispatcher
  6
+from django.utils.functional import curry
4 7
 
5 8
 __all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
6 9
 
@@ -8,25 +11,43 @@
8 11
     settings.DATABASE_ENGINE = 'dummy'
9 12
 
10 13
 try:
11  
-    backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
  14
+    # Most of the time, the database backend will be one of the official 
  15
+    # backends that ships with Django, so look there first.
  16
+    _import_path = 'django.db.backends.'
  17
+    backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
12 18
 except ImportError, e:
13  
-    # The database backend wasn't found. Display a helpful error message
14  
-    # listing all possible database backends.
15  
-    from django.core.exceptions import ImproperlyConfigured
16  
-    import os
17  
-    backend_dir = os.path.join(__path__[0], 'backends')
18  
-    available_backends = [f for f in os.listdir(backend_dir) if not f.startswith('_') and not f.startswith('.') and not f.endswith('.py') and not f.endswith('.pyc')]
19  
-    available_backends.sort()
20  
-    if settings.DATABASE_ENGINE not in available_backends:
21  
-        raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s" % \
22  
-            (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
23  
-    else:
24  
-        raise # If there's some other error, this must be an error in Django itself.
25  
-
26  
-get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, {}, {}, [''])
27  
-get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, {}, {}, [''])
28  
-runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, {}, {}, ['']).runshell()
  19
+    # If the import failed, we might be looking for a database backend 
  20
+    # distributed external to Django. So we'll try that next.
  21
+    try:
  22
+        _import_path = ''
  23
+        backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
  24
+    except ImportError, e_user:
  25
+        # The database backend wasn't found. Display a helpful error message
  26
+        # listing all possible (built-in) database backends.
  27
+        backend_dir = os.path.join(__path__[0], 'backends')
  28
+        available_backends = [f for f in os.listdir(backend_dir) if not f.startswith('_') and not f.startswith('.') and not f.endswith('.py') and not f.endswith('.pyc')]
  29
+        available_backends.sort()
  30
+        if settings.DATABASE_ENGINE not in available_backends:
  31
+            raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s" % \
  32
+                (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
  33
+        else:
  34
+            raise # If there's some other error, this must be an error in Django itself.
29 35
 
  36
+def _import_database_module(import_path='', module_name=''):
  37
+    """Lazyily import a database module when requested."""
  38
+    return __import__('%s%s.%s' % (_import_path, settings.DATABASE_ENGINE, module_name), {}, {}, [''])
  39
+
  40
+# We don't want to import the introspect/creation modules unless 
  41
+# someone asks for 'em, so lazily load them on demmand.
  42
+get_introspection_module = curry(_import_database_module, _import_path, 'introspection')
  43
+get_creation_module = curry(_import_database_module, _import_path, 'creation')
  44
+
  45
+# We want runshell() to work the same way, but we have to treat it a
  46
+# little differently (since it just runs instead of returning a module like
  47
+# the above) and wrap the lazily-loaded runshell() method.
  48
+runshell = lambda: _import_database_module(_import_path, "client").runshell()
  49
+
  50
+# Convenient aliases for backend bits.
30 51
 connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
31 52
 DatabaseError = backend.DatabaseError
32 53
 IntegrityError = backend.IntegrityError
11  docs/settings.txt
@@ -253,9 +253,14 @@ DATABASE_ENGINE
253 253
 
254 254
 Default: ``''`` (Empty string)
255 255
 
256  
-The database backend to use. Either ``'postgresql_psycopg2'``,
257  
-``'postgresql'``, ``'mysql'``,  ``'mysql_old'``, ``'sqlite3'``,
258  
-``'oracle'``, or ``'ado_mssql'``.
  256
+The database backend to use. The build-in database backends are
  257
+``'postgresql_psycopg2'``, ``'postgresql'``, ``'mysql'``, ``'mysql_old'``,
  258
+``'sqlite3'``, ``'oracle'``, or ``'ado_mssql'``.
  259
+
  260
+You can also use a database backend that doesn't ship with Django by
  261
+setting ``DATABASE_ENGINE`` to a fully-qualified path (i.e.
  262
+``mypackage.backends.whatever``). Writing a whole new database backend from
  263
+scratch is left as an exercise to the reader.
259 264
 
260 265
 DATABASE_HOST
261 266
 -------------

0 notes on commit 07447a0

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