I have multiple databases defined. This is for test profile, and I want to be able to specify which database to be picked for testing. eg: "python manage.py test -db=mysql"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
I went through the django documentation, but i cant find a clear cut way of doing it. One way of getting around this is setting up environment variables, and define both databases as default. Then use the db based on the database type.
please let me know if there is a much better way of doing this.
thanks
Amal
You can use the TEST attribute of DATABASES:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'NAME' : 'mysql'
},
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
but is there a reason you don't want to use the default test database django creates?
Related
The django project has multiple apps and they all right now access the same DB. If I want one app which has only read queries to read from read replica will I have to add routers for both DB or creating one router for read replica and alloting it to the app will work? Is there a better way to do this?
you can use multiple database as defined in documentation:
https://docs.djangoproject.com/en/2.0/topics/db/multi-db/
Ex:
DATABASES = {
'default': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'password1'
},
'read_replica': {
'NAME': 'customer_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_cust',
'PASSWORD': 'password2'
}
}
After use a Database Router (django.db.router):
https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#using-routers
There is a DATABASE_ROUTERS config as well.
I am working on a Django project where I need to use both mysql/postgresql as well as mongoDB, one as primary and one as secondary database. How do I configure my db settings to use two databases?
I am able to use 1 database as postgresql or mongoDB, but not able to use both. I have provided the code below of what I have tried.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': os.environ.get("DB_HOST", DB_HOST),
'PORT': os.environ.get('DB_PORT', DB_PORT),
'NAME': os.environ.get("DB_NAME", DB_NAME),
'USER': os.environ.get("DB_USER", DB_USER),
'PASSWORD': os.environ.get("DB_PASSWORD", DB_PASSWORD),
},
}
I have figured out the solution to this. We would need to use a third party plugin called Django MongoDB Engine.
Documentation: https://django-mongodb-engine.readthedocs.io/en/latest/topics/setup.html
'default': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
},
'mongo' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}```
Hi I would like to create a separate empty database for tests. I read on django docs (https://docs.djangoproject.com/en/2.1/topics/testing/overview/)
that :
The default test database names are created by prepending test_ to the value of each NAME in DATABASES. When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!). The TEST dictionary in DATABASES offers a number of settings to configure your test database. For example, if you want to use a different database name, specify NAME in the TEST dictionary for any given database in DATABASES.
So I tried:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'test_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'test_db.sqlite3'),
}
}
but tests still use the default database when running them with
./manage.py test
How can I create and specify a new, empty database for tests purposes?
I think you misread the documentation. Django automatically uses a separate database.
Say your config file looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Then Django will not use the 'default' database, if the name of the database is 'FOO', it will create a database with the name test_FOO. Such that testing and running the Django project should - without of course "patching" this behaviour - not interfere (at least not the databases).
If you however want to specify a different NAME (or other attributes), you can add a 'TEST' key [Django-doc] in the databases, like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'NAME': os.path.join(BASE_DIR, 'other_db.sqlite3'),
}
}
}
I face a problem that I design a information system base on django-admin.
But I have several independent system to build.every system's Features are all the same.
So I want to put a texbox to select to switch my database before I login in admin.How can I do that?
for example ,when I want to login in admin,I want to freely switch from db1 to db2 or reverse.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
},
'newData': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db2.sqlite3'),
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
How can I change the name of the database properly ?