Apothem
Apothem

Reputation: 405

How do I make Django use a different database besides the 'default'?

I am relatively new to Django and one thing that has been on my mind is changing the database that will be used when running the project.

By default, the DATABASES 'default' is used to run my test project. But in the future, I want to be able to define a 'production' DATABASES configuration and have it use that instead.

In a production environment, I won't be able to "manage.py runserver" so I can't really set the settings.

I read a little bit about "routing" the database to use another database, but is there an easier way so that I won't need to create a new router every time I have another database I want to use (e.g. I can have test database, production database, and development database)?

Upvotes: 0

Views: 372

Answers (3)

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13496

There is nothing you can specify in the settings directly. The practice I use is to have addtional setting files for different environments which contain just the settings overwritten which I want to change, like database settings or cache settings for example. My project root application for example would contain the following files on a development environment (attention to the leading underscore):

...
settings.py
settings_dev.py
_settings_test.py
_settings_prod.py
...

Then in settings.py I would add the following lines of code to the beginning:

try:
    from settings_prod import *
except ImportError:
    try:
        from settings_test import *
    except ImportError:
        from settings_dev import *

Since I am on dev environment it will only import my settings_dev file, since the others have a leading underscore.

When I deploy then to a production or testing environment I would rename the relevant files. For production: _settings_prod.py -> settings_prod.py, for testing: _settings_test.py -> settings_test.py. settings_dev.py can basically stay as is, since it will be only imported if the other two fail. The last step you could simply do with automated deployment via fabric or other tools. An example with fabric would be something like run('mv _settings_prod.py settings_prod.py') for renaming.

Upvotes: 0

xiaowl
xiaowl

Reputation: 5217

Why you need a test database? Django create test database automatically before running unittest. And, database routing is not fit your purpose, it's for routing you read/write requests to different database. If you want to use a development database, set up a new DATABASE config in, say local_settings.py, and at the last of your settings.py, type

try:
    from local_settings import *
except ImportError:
    pass

Upvotes: 1

mawimawi
mawimawi

Reputation: 4353

You can just use a different settings.py in your production environment.

Or - which is a bit cleaner - you might want to create a file settings_local.py next to settings.py where you define a couple of settings that are specific for the current machine (like DEBUG, DATABASES, MEDIA_ROOT etc.) and do a from settings_local import * at the beginning of your generic settings.py file. Of course settings.py must not overwrite these imported settings.

Upvotes: 1

Related Questions