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
34👍
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.
- Graphene-python performance issues for large data sets
- Django-rest-swagger nested serializers with readonly fields not rendered properly
- Django Admin + FORCE_SCRIPT_NAME + Login redirects incorrectly
- How can my Model primary key start with a specific number?
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
- Appropriate choice of authentication class for python REST API used by web app
- Django – inlineformset_factory with more than one ForeignKey
- How to display "x days ago" type time using Humanize in Django template?
6👍
@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]
}