In my Django project I want to use a queryset method that is only available in postgresql, if postgresql is being used.
How can I check the database from settings.DATABASES?
Assuming this structure:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # could be: 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'
My python skills are too weak to traverse that structure of dictionaries =(
If you're interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MariaDB, MySQL), .tables (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.
Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.
sqlite3′ in your project directory. The file is a database file that will keep all of the data that you will be generating. Since Django is a server-side framework, it treats your computer as the host when you run the server from the command line or terminal.
This gives you the name of database backend configured as default
in settings.DATABASES
:
>>> from django.db import connection
>>> print connection.vendor
'sqlite'
In case you have multiple databases configured:
>>> from django.db import connections
>>> print connections['default'].vendor
'mysql'
>>> print connections['reporting'].vendor
'postgresql'
from django.conf import settings
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
# happy coding
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