Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Delayed settings.DATABASES usage by ConnectionHandler

Refs #20474.
  • Loading branch information...
commit 2d8c132b187d9dfefe0f409c4fb39b54a6645675 1 parent 499a745
Claude Paroz claudep authored
Showing with 19 additions and 12 deletions.
  1. +1 −6 django/db/__init__.py
  2. +18 −6 django/db/utils.py
7 django/db/__init__.py
View
@@ -2,7 +2,6 @@
from django.conf import settings
from django.core import signals
-from django.core.exceptions import ImproperlyConfigured
from django.db.utils import (DEFAULT_DB_ALIAS,
DataError, OperationalError, IntegrityError, InternalError,
ProgrammingError, NotSupportedError, DatabaseError,
@@ -13,11 +12,7 @@
__all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
'IntegrityError', 'DEFAULT_DB_ALIAS')
-
-if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
- raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
-
-connections = ConnectionHandler(settings.DATABASES)
+connections = ConnectionHandler()
router = ConnectionRouter(settings.DATABASE_ROUTERS)
24 django/db/utils.py
View
@@ -6,6 +6,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
+from django.utils.functional import cached_property
from django.utils.importlib import import_module
from django.utils.module_loading import import_by_path
from django.utils._os import upath
@@ -138,16 +139,27 @@ class ConnectionDoesNotExist(Exception):
class ConnectionHandler(object):
- def __init__(self, databases):
- if not databases:
- self.databases = {
+ def __init__(self, databases=None):
+ """
+ databases is an optional dictionary of database definitions (structured
+ like settings.DATABASES).
+ """
+ self._databases = databases
+ self._connections = local()
+
+ @cached_property
+ def databases(self):
+ if self._databases is None:
+ self._databases = settings.DATABASES
+ if self._databases == {}:
+ self._databases = {
DEFAULT_DB_ALIAS: {
'ENGINE': 'django.db.backends.dummy',
},
}
- else:
- self.databases = databases
- self._connections = local()
+ if DEFAULT_DB_ALIAS not in self._databases:
+ raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
+ return self._databases
def ensure_defaults(self, alias):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.