7👍
There are several options available:
-
Below is a very cheap solution. Django always selects the database called ‘default’. You can assign it conditionally in
settings.py
:DATABASES = { 'dev': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', }, 'production': { 'ENGINE': 'django.db.backends.postgresql', # ... }, } DATABASES['default'] = DATABASES['dev' if DEBUG else 'production']
-
You can implement an alternate settings module called
settings_dev.py
. Configure database there and use the environment variableDJANGO_SETTINGS_MODULE
to point toyourapp.settings_dev
. -
Implementing a custom database router. This is almost certainly overkill for many use-cases. See the Django documentation on multiple database support.
Source:stackexchange.com