Delayed settings.DATABASE_ROUTERS usage by ConnectionRouter
Refs #20474.
This commit is contained in:
parent
2d8c132b18
commit
6a6bb168be
|
@ -1,6 +1,5 @@
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.core import signals
|
from django.core import signals
|
||||||
from django.db.utils import (DEFAULT_DB_ALIAS,
|
from django.db.utils import (DEFAULT_DB_ALIAS,
|
||||||
DataError, OperationalError, IntegrityError, InternalError,
|
DataError, OperationalError, IntegrityError, InternalError,
|
||||||
|
@ -14,7 +13,7 @@ __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
|
||||||
|
|
||||||
connections = ConnectionHandler()
|
connections = ConnectionHandler()
|
||||||
|
|
||||||
router = ConnectionRouter(settings.DATABASE_ROUTERS)
|
router = ConnectionRouter()
|
||||||
|
|
||||||
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
|
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
|
||||||
# for backend bits.
|
# for backend bits.
|
||||||
|
|
|
@ -214,14 +214,23 @@ class ConnectionHandler(object):
|
||||||
|
|
||||||
|
|
||||||
class ConnectionRouter(object):
|
class ConnectionRouter(object):
|
||||||
def __init__(self, routers):
|
def __init__(self, routers=None):
|
||||||
self.routers = []
|
"""
|
||||||
for r in routers:
|
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):
|
if isinstance(r, six.string_types):
|
||||||
router = import_by_path(r)()
|
router = import_by_path(r)()
|
||||||
else:
|
else:
|
||||||
router = r
|
router = r
|
||||||
self.routers.append(router)
|
self._routers.append(router)
|
||||||
|
return self._routers
|
||||||
|
|
||||||
def _router_func(action):
|
def _router_func(action):
|
||||||
def _route_db(self, model, **hints):
|
def _route_db(self, model, **hints):
|
||||||
|
|
Loading…
Reference in New Issue