Dor*_*ora 2 python mysql database django settings
我不是要检查连接的 db 名称,我在 django 中设置了多个数据库,并且还使用它database route
来创建一个,failover
但我想知道是否可以获取连接主机的名称或指定给设置的名称。 .
例如
DATABASES = {
'default': { # trying to get this name 'default'
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'euser',
'PASSWORD': 'password',
'HOST': 'host', # trying to get this host name 'host'
'PORT': 3306,
},
'node2': { # trying to get this name 'node2'
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'euser',
'PASSWORD': 'password',
'HOST': 'host2', # trying to get this host name 'host2'
'PORT': 3306,
},
}
Run Code Online (Sandbox Code Playgroud)
有没有可能得到我上面注释掉的那些名字?对于我的故障转移,如果正在使用另一个数据库,我会尝试发送电子邮件,但如果我能得到特别是host
这样就更好了,这对我来说会更容易
PS题外话,名字可以改成default
其他名字吗?我尝试更改它,但会出错。似乎我必须至少有一个名为的数据库设置名称default
在此先感谢您的帮助。
所以,这样做:
import pprint # Makes it sooo pretty :)
from django.db import connection
print(pprint.pformat(connection.settings_dict))
Run Code Online (Sandbox Code Playgroud)
将为您提供当前的连接设置。例如,上面的代码应该打印如下内容:
{'ATOMIC_REQUESTS': False,
'AUTOCOMMIT': True,
'CONN_MAX_AGE': 600,
'ENGINE': 'django.db.backends.postgresql',
'HOST': 'localhost',
'NAME': 'stack_overflow',
'OPTIONS': {},
'PASSWORD': '***',
'PORT': '5432',
'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None, 'NAME': None},
'TEST_NAME': 'stack_overflow_test',
'TIME_ZONE': None,
'USER': 'postgres'}
Run Code Online (Sandbox Code Playgroud)
所以你应该能够通过执行访问主机名 connection.settings_dict['HOST']
归档时间: |
|
查看次数: |
1268 次 |
最近记录: |