Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Delayed settings.DATABASE_ROUTERS usage by ConnectionRouter

Refs #20474.
  • Loading branch information...
commit 6a6bb168be90594a18ab6d62c994889b7e745055 1 parent 2d8c132
@claudep claudep authored
Showing with 14 additions and 6 deletions.
  1. +1 −2  django/db/__init__.py
  2. +13 −4 django/db/utils.py
View
3  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 @@
connections = ConnectionHandler()
-router = ConnectionRouter(settings.DATABASE_ROUTERS)
+router = ConnectionRouter()
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits.
View
17 django/db/utils.py
@@ -214,14 +214,23 @@ def all(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.