Django

Code

Changeset 6316

Show
Ignore:
Timestamp:
09/15/07 14:25:20 (8 months ago)
Author:
jacob
Message:

Fixed #5454: settings.DATABASE_BACKEND may now refer to an external package (i.e. one located outside the Django source. Thanks, George Vilches.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/__init__.py

    r5076 r6316  
     1import os 
    12from django.conf import settings 
    23from django.core import signals 
     4from django.core.exceptions import ImproperlyConfigured 
    35from django.dispatch import dispatcher 
     6from django.utils.functional import curry 
    47 
    58__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError') 
     
    912 
    1013try: 
    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), {}, {}, ['']) 
    1218except 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. 
     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. 
    2535 
    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(
     36def _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), {}, {}, ['']
    2939 
     40# We don't want to import the introspect/creation modules unless  
     41# someone asks for 'em, so lazily load them on demmand. 
     42get_introspection_module = curry(_import_database_module, _import_path, 'introspection') 
     43get_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. 
     48runshell = lambda: _import_database_module(_import_path, "client").runshell() 
     49 
     50# Convenient aliases for backend bits. 
    3051connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS) 
    3152DatabaseError = backend.DatabaseError 
  • django/trunk/docs/settings.txt

    r6191 r6316  
    254254Default: ``''`` (Empty string) 
    255255 
    256 The database backend to use. Either ``'postgresql_psycopg2'``, 
    257 ``'postgresql'``, ``'mysql'``,  ``'mysql_old'``, ``'sqlite3'``, 
    258 ``'oracle'``, or ``'ado_mssql'``. 
     256The database backend to use. The build-in database backends are 
     257``'postgresql_psycopg2'``, ``'postgresql'``, ``'mysql'``, ``'mysql_old'``, 
     258``'sqlite3'``, ``'oracle'``, or ``'ado_mssql'``. 
     259 
     260You can also use a database backend that doesn't ship with Django by 
     261setting ``DATABASE_ENGINE`` to a fully-qualified path (i.e. 
     262``mypackage.backends.whatever``). Writing a whole new database backend from 
     263scratch is left as an exercise to the reader. 
    259264 
    260265DATABASE_HOST