insert in database that is not the default DJANGO - python

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.

Related

Is need to change DB in unit test cases - python django

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')

Multiple Databases --Migrations

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

Want to implement multiple database in django project

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'

How to use other than default database

I am creating a project that supports multiple databases. I have written queries in models, and I need to save those details in a selected database.
I have two databases: SQLite3 and MongoDB. Now I need to save those query details in SQLite3. How do I do it? I am using Python 2.7 and Django 1.5.4.
Here is the code in settings.py:
DATABASES = {
'default': {},
'sqlite':
{
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(APP_DIR, 'mydb.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'mongodb':
{
'ENGINE': 'django_mongodb_engine',
'NAME': "mydb",
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
And here is the code in models.py:
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
Use using argument if you use save method.
blog = Blog(name='This is a blog post', tagline='python django')
blog.save(using='sqlite')
or use db_manager if you use create method:
blog = Blog.objects.db_manager('sqlite').create(
name='This is a blog post', tagline='python django')
For other operations, see Multiple databases - Django documentation.

How to use a specific database for a single test in django?

Does anyone kno how to use a specific database for a single test in Django?
EDIT: At the moment, i'm using sqlite for testing, but, i want to use a mysql database for a specific test (def test_benchmark...), which will benchmark some sql statements.
Thanks
I don't know of any builtin way to do it - but I didn't look for it neither honestly. One possible (dirty) hack is to check what's in sys.argv in your settings (which is just another Python module) and have an alternate database settings for your particular test, ie:
# settings.py
import os, sys
DATABASES = {
'default': {
'NAME': 'db_name',
'ENGINE': 'your.engine',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
}
}
if "test" in sys.argv and "your-test-name" in sys.argv:
DATABASES = {
'default': {
'NAME': 'other_db_name',
'ENGINE': 'your.engine',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
}
}
In Django you set the database in your settings.py file. The configuration should look something like this:
DATABASES = {
'default': {
'NAME': 'db_name',
'ENGINE': 'sql_server.pyodbc',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
'OPTIONS' : { "use_mars" : False,
"provider" : "SQLNCLI10",
"extra_params" : "MARS Connection=True"},
}
}

Categories

Resources