I am not trying to check the db name to the connection, I have multiple database setup in django and also used the database route
to create a failover
but I am wondering if I can get the name of the connection host or the name given to the settings...
for example
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,
},
}
Is it possible to get those names I commented out above?
For my failover, I am trying to send an email if another db is being used, but it would be great if I can get especially the host
so it would be easier on me
P.S. off topic, is it possible to change the name default
to other names? I tried changing it and I would get errors. Seems like I must have at least one database setting name called default
Thanks in advance for any help.
So, doing:
import pprint # Makes it sooo pretty :)
from django.db import connection
print(pprint.pformat(connection.settings_dict))
Will give you the current connection settings. For instance, the code above should print something like that:
{'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'}
So you should be able to access the hostname just by doing connection.settings_dict['HOST']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With