19

I'm using Django 1.4

I need to start the development server and I want to specify (in the command) which database it must use. For example if my settings contains:

DATABASES = {
    'default': {
        'NAME': 'db.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
    'tests': {
        'NAME': 'tests.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
}

I want to use something like:

django-admin.py runserver --database=tests

There is something similar? I also tried to use a second setting file but the --settings option doesn't work: (--settings option seems to be deprecated, in DOCS there is no mention at all)

django-admin.py runserver --settings=settings_tests

or

django-admin.py runserver --settings settings_tests

raises an error:

ImportError: Could not import settings 'settings_tests' (Is it on sys.path?): No module named settings_tests

4 Answers 4

35

A cleaner and more scalable way to switch configurations than to create several config files would be to use environment variables (see #3 of the twelve-factor app methodology used by Heroku and others). For example:

from os import environ

DATABASES = {
    'main': {
        'NAME': 'db.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
    'tests': {
        'NAME': 'tests.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
}

default_database = environ.get('DJANGO_DATABASE', 'main')
DATABASES['default'] = DATABASES[default_database]

You can then change the default database by setting the DJANGO_DATABASE environment variable.

export DJANGO_DATABASE='tests'
./manage.py runserver

...or...

DJANGO_DATABASE='tests' ./manage.py runserver

You could also set environment variables using Python code.


Edit: To make this process easier, Kenneth Reitz has written a nice little app called dj-database-url.

2
  • Interesting, but I prefer to use the --settings option. Thanks
    – Griffosx
    Jun 25, 2012 at 13:38
  • 1
    This solution helped me to keep same settings.py file for production and development, with database for production being chosen by setting an environment variable outside the application. Awesome!
    – shaffooo
    Jun 1, 2017 at 17:03
12

I discovered that the right command to call in Django 1.4 is:

django-admin.py runserver --settings=myproject.settings_tests

Where is this information in the Django DOCS?

Thanks for all your response

Griffosx

7

@Danilo Barges put a simple way to solve the problem. I only add a few change to deal with running tests. That's because DATABASES['default'] = DATABASES[default_database] will add an entry to DATABASES dictionary. So if you run tests the test runner will run against 'default' and the next entry in DATABASES. Use two dictionaries instead:

DATABASES_AVAILABLE = {
    'default': {
        'NAME': 'db.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
    'tests': {
        'NAME': 'tests.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },
}

database = os.environ.get('DJANGO_DATABASE', 'main')
DATABASES = {
    'default': DATABASES_AVAILABLE[database]
}
6

Create settings_tests.py with following:

from settings import *

DATABASES = {
    'default': {
        'NAME': 'tests.db',
        'ENGINE': 'django.db.backends.sqlite3'
    },

}

Execute django-admin.py runserver --settings=settings_tests

6
  • It doesn't work. I already tried this solution but it raises the exception above. I am using Django 1.4
    – Griffosx
    Jun 25, 2012 at 10:30
  • how can you get exception "no module" it you just created that module?
    – vartec
    Jun 25, 2012 at 10:33
  • Well, actually the error is ImportError: Could not import settings 'settings_tests' (Is it on sys.path?): No module named settings. I already added the file settings_test.py in the root project directory (at the same level of settings.py). The error is about a module not about a file...
    – Griffosx
    Jun 25, 2012 at 10:49
  • A file is a module. The problem is that your settings_test.py file could not import the settings module. Do you have a settings.py file next to your settings_tests.py file? Jun 25, 2012 at 12:30
  • Django 1.4 changed the default directory structure. The settings.py file is now in a subfolder, not on the same level as manage.py. In that case, use from myapp.settings import * instead of from settings import * (myapp corresponds to the name of that folder of course). Jun 25, 2012 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.