summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2013-05-21 12:21:31 +0200
committerClaude Paroz <claude@2xlibre.net>2013-05-23 15:19:12 +0200
commit6a6bb168be90594a18ab6d62c994889b7e745055 (patch)
treed883256bc785f46ae47affadf9716d0dabf6d79c /django
parent2d8c132b187d9dfefe0f409c4fb39b54a6645675 (diff)
Delayed settings.DATABASE_ROUTERS usage by ConnectionRouter
Refs #20474.
Diffstat (limited to 'django')
-rw-r--r--django/db/__init__.py3
-rw-r--r--django/db/utils.py17
2 files changed, 14 insertions, 6 deletions
diff --git a/django/db/__init__.py b/django/db/__init__.py
index 885ca41318..2421ddeab8 100644
--- a/django/db/__init__.py
+++ b/django/db/__init__.py
@@ -1,6 +1,5 @@
import warnings
-from django.conf import settings
from django.core import signals
from django.db.utils import (DEFAULT_DB_ALIAS,
DataError, OperationalError, IntegrityError, InternalError,
@@ -14,7 +13,7 @@ __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
connections = ConnectionHandler()
-router = ConnectionRouter(settings.DATABASE_ROUTERS)
+router = ConnectionRouter()
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits.
diff --git a/django/db/utils.py b/django/db/utils.py
index 0bc72fd768..6ba6bf346c 100644
--- a/django/db/utils.py
+++ b/django/db/utils.py
@@ -214,14 +214,23 @@ 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
+ for r in self._routers:
if isinstance(r, six.string_types):
router = import_by_path(r)()
else:
router = r
- self.routers.append(router)
+ self._routers.append(router)
+ return self._routers
def _router_func(action):
def _route_db(self, model, **hints):