try:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'jobstar',
}
}
except:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'jobstar'),
}
}
我如何处理django中数据库设置的异常。如图所示,我的主数据库是mongodb。我想当mongo未运行或未安装在系统中时,将使用sqlite(辅助)数据库。
我正在尝试以上述方式执行此操作,这会向我抛出错误。 请看一下我的代码
t__
self.loader = MigrationLoader(self.connection)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 206, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
if self.has_table():
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 56, in table_names
return get_names(cursor)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 51, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/djongo/introspection.py", line 46, in get_table_list
for c in cursor.db_conn.collection_names(False)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/database.py", line 715, in collection_names
nameOnly=True, **kws)]
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/database.py", line 674, in list_collections
read_pref) as (sock_info, slave_okay):
File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1099, in _socket_for_reads
server = topology.select_server(read_preference)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/topology.py", line 224, in select_server
address))
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/topology.py", line 183, in select_servers
selector, server_timeout, address)
File "/home/soubhagya/Desktop/worke/rmaze-partner/rmaze-partner-env/lib/python3.6/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
答案 0 :(得分:0)
如果我对您的理解是正确的,那么您将尝试获取故障转移数据库,以获取默认数据库不可用的情况。
请参阅本指南https://n3tc4t.github.io/blog/simple-dbha-for-django-app