103π
To get the db name with recent Django versions (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.
18π
Update: the answer below is for older Django versions. For up to date approach check the answer by Rems:
from django.db import connection
db_name = connection.settings_dict['NAME']
In older Django versions, 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.
- [Django]-How to obtain a QuerySet of all rows, with specific fields for each one of them?
- [Django]-Variable subtraction in django templates
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
16π
Tested in django 1.9
Go into the shell.
./manage.py shell
Check your databases.
from django import db
db.connections.databases
- [Django]-How to fix " AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link' " error in Django
- [Django]-Celery. Decrease number of processes
- [Django]-Django-taggit β how do I display the tags related to each record
15π
The answer seems to be obsolete.
In django 1.6 you can do:
from django import db
print db.connections.databases
- [Django]-Check if an object exists
- [Django]-How to change status of JsonResponse in Django
- [Django]-Could not find a version that satisfies the requirement pkg-resources==0.0.0
13π
IMO, the best way to do this, is the following undocumented django function:
>>> from django.db import connection
>>> connection.vendor
'postgresql' or 'sqlite'
- [Django]-How to run celery as a daemon in production?
- [Django]-Temporarily disable auto_now / auto_now_add
- [Django]-Django model object with foreign key creation
2π
Try putting the following in your settings file:
import sys
management_command = sys.argv[1] if len(sys.argv) > 1 else ""
TESTING = management_command.startswith("test")
If TESTING is true you are running with python manage.py test myapp.
edit: You probably could put that anywhere, not necessarily your settings file. Give it a go and see how it works!
- [Django]-How to log all sql queries in Django?
- [Django]-Create Django model or update if exists
- [Django]-Numeric for loop in Django templates
2π
What worked excellent for me (the dictionary was empty in my case because Iβm using a read_default_file
file in settings.py
) is just executing the following SQL query
SELECT DATABASE()
- [Django]-Are Django SECRET_KEY's per instance or per app?
- [Django]-Django β How to pass several arguments to the url template tag
- [Django]-How to compare dates in Django templates
2π
In Django >= 1.10 version
Use:
from django.conf import settings
db_name = settings.DATABASES['default']['NAME']
- [Django]-How do you set DEBUG to True when running a Django test?
- [Django]-Remove pk field from django serialized objects
- [Django]-How to get an ImageField URL within a template?
1π
In Django >= 1.11
Use:
from django import db
db_name = db.utils.settings.DATABASES['default']['NAME']
- [Django]-Django model method β create_or_update
- [Django]-How can I use the variables from "views.py" in JavasScript, "<script></script>" in a Django template?
- [Django]-Django : How can I find a list of models that the ORM knows?
1π
In case of mysql, raw query through django, can be a solution too.
from django.db import connection
cursor = connection.cursor()
cursor.execute('select database()')
row = cursor.fetchone()
print row[0]
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Running "unique" tasks with celery
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
1π
To get database conf and NAME, you can use any of the models in any app
from someapp.models import ExampleModels
current_DB = ExampleModels.objects.db
print (current_DB)
- [Django]-Pytest.mark.parametrize with django.test.SimpleTestCase
- [Django]-How to combine multiple QuerySets in Django?
- [Django]-Is it possible to use FastAPI with Django?