summaryrefslogtreecommitdiff
path: root/django/db/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/utils.py')
-rw-r--r--django/db/utils.py45
1 files changed, 33 insertions, 12 deletions
diff --git a/django/db/utils.py b/django/db/utils.py
index e84060f9b3..bd7e10d24c 100644
--- a/django/db/utils.py
+++ b/django/db/utils.py
@@ -6,6 +6,7 @@ import warnings
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
@@ -90,8 +91,7 @@ class DatabaseErrorWrapper(object):
except AttributeError:
args = (exc_value,)
dj_exc_value = dj_exc_type(*args)
- if six.PY3:
- dj_exc_value.__cause__ = exc_value
+ dj_exc_value.__cause__ = exc_value
# Only set the 'errors_occurred' flag for errors that may make
# the connection unusable.
if dj_exc_type not in (DataError, IntegrityError):
@@ -138,16 +138,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):
"""
@@ -202,14 +213,24 @@ class ConnectionHandler(object):
class ConnectionRouter(object):
- def __init__(self, routers):
- self.routers = []
- for r in routers:
+ def __init__(self, routers=None):
+ """
+ If routers is not specified, will default to settings.DATABASE_ROUTERS.
+ """
+ self._routers = routers
+
+ @cached_property
+ def routers(self):
+ if self._routers is None:
+ self._routers = settings.DATABASE_ROUTERS
+ routers = []
+ for r in self._routers:
if isinstance(r, six.string_types):
router = import_by_path(r)()
else:
router = r
- self.routers.append(router)
+ routers.append(router)
+ return routers
def _router_func(action):
def _route_db(self, model, **hints):