def get_connection(cls): ''' This method is multi-db ready. It uses your site's db router to determine which database the view is in. ''' db = ConnectionRouter().db_for_read(cls()) return connections[db]
OperationalError, ProgrammingError, ) __all__ = [ 'backend', 'connection', 'connections', 'router', 'DatabaseError', 'IntegrityError', 'InternalError', 'ProgrammingError', 'DataError', 'NotSupportedError', 'Error', 'InterfaceError', 'OperationalError', 'DEFAULT_DB_ALIAS', 'DJANGO_VERSION_PICKLE_KEY' ] # 管理各种alias的请求 connections = ConnectionHandler() # 路由 router = ConnectionRouter() # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases # for backend bits. # DatabaseWrapper.__init__() takes a dictionary, not a settings module, so we # manually create the dictionary from the settings, passing only the settings # that the database backends care about. # We load all these up for backwards compatibility, you should use # connections['default'] instead. class DefaultConnectionProxy(object): """ Proxy for accessing the default DatabaseWrapper object's attributes. If you need to access the DatabaseWrapper object itself, use connections[DEFAULT_DB_ALIAS] instead.
elif database['ENGINE'] == 'sqlite3': full_engine = 'django.contrib.gis.db.backends.spatialite' else: full_engine = 'django.contrib.gis.db.backends.%s' % database[ 'ENGINE'] else: warnings.warn( "Short names for ENGINE in database configurations are deprecated. " "Prepend %s.ENGINE with 'django.db.backends.'" % alias, DeprecationWarning) full_engine = "django.db.backends.%s" % database['ENGINE'] database['ENGINE'] = full_engine connections = ConnectionHandler(settings.DATABASES) router = ConnectionRouter(settings.DATABASE_ROUTERS) # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases # for backend bits. # DatabaseWrapper.__init__() takes a dictionary, not a settings module, so # we manually create the dictionary from the settings, passing only the # settings that the database backends care about. Note that TIME_ZONE is used # by the PostgreSQL backends. # we load all these up for backwards compatibility, you should use # connections['default'] instead. connection = connections[DEFAULT_DB_ALIAS] backend = load_backend(connection.settings_dict['ENGINE']) # Register an event that closes the database connection
def clear_routers_cache(**kwargs): if kwargs['setting'] == 'DATABASE_ROUTERS': router.routers = ConnectionRouter().routers
def clear_routers_cache(**kwargs): if kwargs["setting"] == "DATABASE_ROUTERS": router.routers = ConnectionRouter().routers
def clear_routers_cache(*, setting, **kwargs): if setting == "DATABASE_ROUTERS": router.routers = ConnectionRouter().routers