Ticket #10487: conn.diff

File conn.diff, 3.7 KB (added by Alex Gaynor, 16 years ago)
  • django/db/__init__.py

    diff --git a/django/db/__init__.py b/django/db/__init__.py
    index 517fec6..9f5bdf7 100644
    a b __all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')  
    99if not settings.DATABASE_ENGINE:
    1010    settings.DATABASE_ENGINE = 'dummy'
    1111
    12 try:
    13     # Most of the time, the database backend will be one of the official
    14     # backends that ships with Django, so look there first.
    15     _import_path = 'django.db.backends.'
    16     backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
    17 except ImportError, e:
    18     # If the import failed, we might be looking for a database backend
    19     # distributed external to Django. So we'll try that next.
     12def load_backend(path):
    2013    try:
    21         _import_path = ''
    22         backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
    23     except ImportError, e_user:
    24         # The database backend wasn't found. Display a helpful error message
    25         # listing all possible (built-in) database backends.
    26         backend_dir = os.path.join(__path__[0], 'backends')
     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        return __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
     18    except ImportError, e:
     19        # If the import failed, we might be looking for a database backend
     20        # distributed external to Django. So we'll try that next.
    2721        try:
    28             available_backends = [f for f in os.listdir(backend_dir)
    29                     if os.path.isdir(os.path.join(backend_dir, f))]
    30         except EnvironmentError:
    31             available_backends = []
    32         available_backends.sort()
    33         if settings.DATABASE_ENGINE not in available_backends:
    34             raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
    35                 (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user)
    36         else:
    37             raise # If there's some other error, this must be an error in Django itself.
     22            _import_path = ''
     23            return __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            try:
     29                available_backends = [f for f in os.listdir(backend_dir)
     30                        if os.path.isdir(os.path.join(backend_dir, f))]
     31            except EnvironmentError:
     32                available_backends = []
     33            available_backends.sort()
     34            if settings.DATABASE_ENGINE not in available_backends:
     35                raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
     36                    (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user)
     37            else:
     38                raise # If there's some other error, this must be an error in Django itself.
    3839
    3940# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
    4041# for backend bits.
    4142
     43backend = load_backend(settings.DATABASE_ENGINE)
     44
    4245# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
    4346# we manually create the dictionary from the settings, passing only the
    4447# settings that the database backends care about. Note that TIME_ZONE is used
    4548# by the PostgreSQL backends.
     49
    4650connection = backend.DatabaseWrapper({
    4751    'DATABASE_HOST': settings.DATABASE_HOST,
    4852    'DATABASE_NAME': settings.DATABASE_NAME,
Back to Top