How to know current name of the database in Django?
Solution 1:
To get the db name with recent Django versions (tried with 1.8):
from django.db import connection
db_name = connection.settings_dict['NAME']
# Or alternatively
# db_name = connection.get_connection_params()['db']
Be mindful of reading this value after initialization, so that it has the correct value when running unit tests.
Solution 2:
You can check it in db.settings
:
from django import db
db.settings.DATABASES['default']['NAME']
To see the database used to fetch a specific object you can do:
object._state.db
This will give you the database key in config, such as 'default', so if you have multiple databases in config you can check the right one.
When you run tests, db.settings
should be updated to contain the test-specific database name.
Solution 3:
Tested in django 1.9
Go into the shell.
./manage.py shell
Check your databases.
from django import db
db.connections.databases
Solution 4:
The answer seems to be obsolete.
In django 1.6 you can do:
from django import db
print db.connections.databases