I want to use multiple sqlite3 database for my app. I want to write some of the data(which is user log) to one database and rest of the stuff to another db. After that I want to read from both the database.
Thanks
DATABASE_ROUTERS = ['manager.router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = { 'db_b':'db_b'}
DATABASES = {
'default': {
'ENGINE': 'backend_of_your_choice',
'NAME': 'default',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': '127.0.0.1',
'PORT': '',
},
'db_b': {
'ENGINE': 'backend_of_your_choice',
'NAME': 'db_b',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': '127.0.0.1',
'PORT': '',
},}
Then in your model MetaClass define the following for all the models which you want to use db_b:
class Meta:
app_label = 'db_b'
Related
I'm trying to perform unit tests on Django with db mocking by mongomock.
My connection to the DB is made using settings.py:
DATABASES = {
'dbname': {
'ENGINE': 'djongo',
'NAME': 'db1',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': DB_HOST,
'port': DB_PORT,
'username': DB_USERNAME,
'password': DB_PASSWORD,
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1'
}
},
'default': {
'ENGINE': 'djongo',
'NAME': 'users',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': DB_HOST,
'port': DB_PORT,
'username': DB_USERNAME,
'password': DB_PASSWORD,
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1'
}
}
}
and the test looks like this:
from django.contrib.auth.models import User
class BaseApiTest():
def test_access_login(self):
User.objects.create_user(
username='admin',
password='admin'
)
....
self.assertEqual(AccessAttempt.objects.count(), 1)
Here there is an internal connection of Django to the DB. (when its trying to access django.contrib.auth.models.User)
Any ideas?
The goal is to run the tests without a DB instance.
In other words, how can I overwrite the Django connection (self.databases) to the DB using a mongomock connection...
I am creating unit test cases now.. I create delete API.. Its working fine with Post Man. Its delete the the Record... But Its do not delete Report when I run this in test case.. Its wrong something or its normal behaviour in test cases. I am creating this test cases in python..
Thanks in Advance
As i remember, you can add multiple connections in settings.py file
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': 'db_prod',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': 'http://example.com',
'PORT': '5432',
},
'test': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': 'db_test',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': 'http://example.com',
'PORT': '5432',
}
}
Then use it on model like this Model.objects.using('test')
I know django works with multiple database connections, I need to insert in a database that is not the default, but I get the following error.
ProgrammingError at /api-jde/f59presapi/2279/
(1146, 'Table \'dblocal."oracle"."FPRES"\' doesn\'t exist')
this is the configuration of my settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dblocal',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'bd2': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'XXXX',
'USER': 'oracle',
'PASSWORD': 'xxxxx',
},
}
the bd in which I need to insert is the db2 (I'm not putting all the data here but if I have them full), in the created model I have the METa configuration with the following:
class Meta:
managed = False
db_table = u'"oracle"."FPRES"'
db = DATABASES['db2']
Perhaps this will help if added to the meta class.
As far as I know you can't specify a database for a specific model. You need to do something like this every time:
YourModel.objects.using('bd2').all()
Hope this helps.
I am using 2 postgres databases in my django app.
'newpostgre': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre2',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
'newpostgre2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre3',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
I have a very simple model
class Check1(models.Model):
title = models.CharField(max_length=100)
I had run
python manage.py migrate --database=newpostgre
python manage.py migrate --database=newpostgre2
when I open my new_postgre2(for newpostgre) database in postgre, I can see my Check1 table there.
But in my new_postgre3(for newpostgre2) database in postgre, no Check1 table is there, only those initial migrations are there.
Why I can't see my table in new_postgre3 when migrations have been successfully made?
Because The same object name can be used in different schemas sometimes conflict. The best practice allow many users to use one database without interfering with each other. Use this change second database user and check again. I think it is work.
'newpostgre': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre2',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
'newpostgre2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre3',
'USER': 'tester_different',
'PASSWORD': 'password_different',
'HOST': 'localhost',
'PORT': '',
},
python manage.py migrate --database=newpostgre2
I've got a problem with Django DATABASES setting in settings.py.
I'm using Python 3.4.3, Django 1.10.5 and psycopg 2.6.1
if my code looks like
DATABASES = {
'default':{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': ' dbname',
'USER': 'postgres',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '5432',
}
}
and i run manage.py diffsettings in DATABASE section there are some default settings:
DATABASES = {'default': {'USER': '', 'HOST': '', 'AUTOCOMMIT': True, 'NAME': '', 'ATOMIC_REQUESTS': False, 'OPTIONS': {}, 'TEST': {'MIRROR': None, 'CHARSET': None, 'NAME': None, 'COLLATION': None}, 'ENGINE': 'django.db.backends.dummy', 'PASSWORD': '', 'CONN_MAX_AGE': 0, 'TIME_ZONE': None, 'PORT': ''}}
but when I change 'default' to 'db' (or any other name)
DATABASES = {
'db':{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': ' dbname',
'USER': 'postgres',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '5432',
}
}
the manage.py diffsettings 'knows' the database db:
DATABASES = {'db': {'NAME': 'dbname', 'HOST': 'localhost', 'PORT': '5432', 'PASSWORD': 'dbpass', 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'USER': 'postgres'}, 'default': {'OPTIONS': {}, 'ATOMIC_REQUESTS': False, 'HOST': '', 'AUTOCOMMIT': True, 'PASSWORD': '', 'USER': '', 'TIME_ZONE': None, 'NAME': '', 'PORT': '', 'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.dummy', 'TEST': {'NAME': None, 'MIRROR': None, 'COLLATION': None, 'CHARSET': None}}}
What should I to to use my database as a default and don't have to use DATABASE_ROUTERS ?
From your comment in the question above, I'm assuming that you're trying to set up Django in a Heroku instance.
If you're using Heroku's starter template, you're going to see an overwrite to the DATABASES variable. This is because Heroku uses the library dj_database_url to get the environment path of the database that was deployed with your Heroku app. So Django updates the default database to use it. This is relevant when you want to deploy your Django app to the Heroku instance.
In case you want to use your app in your local environment (your machine), you just need to comment those two lines:
# db_from_env = dj_database_url.config(conn_max_age=500)
# DATABASES['default'].update(db_from_env)
But remember to comment out those lines again when uploading your code to Heroku.