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
The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.
The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.
sqlite3'. The file is database file where all the data that you will be generating will be stored. It is a local file as Django is a server-side framework and it treats your computer as the host when you actually run the server in command line/terminal.
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.
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
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
@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]
}
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