Django SQL setting up a default schema to all the tables - python

I am setting up a local environment for a project which is built with DJANGO+React and SQL Server
I had my SQL connection set up like this
DATABASES={
'default': {
'ENGINE':'sql_server.pyodbc',
'NAME':'Test_Db',
'USER':'Test_User',
'PASSWORD':'************',
'HOST':'*******',
'PORT':'****',
'OPTIONS': {
'dsn':'FreeTDS',
'autocommit':True,
}
}
}
I was able to connect to database but for some reason there were new tables created back in database with new schema dbo and all the tables are empty.
But I have all the user data in different schema app. where I need to connect and perform all the operations.
When I am running the local server it's referencing it to DBO where I don't have any data which resulting login errors.
Is there any way I can set in manage.py or setting.py to consider app schema for all the models defined.

Try with the search_path in your DATABASES, in settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS': {
'options': '-c search_path=first_schema,second_schema,third_schema'
},
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
}
Add your desired schema to be in the first place.

Related

Can I access other MySQL database in Python Django without having access to its models?

I have this connection to MySQL Workbench database. The thing is, the database and tables isn't made by my Django project so I don't have any access to its class models. Is there any way I can add, update or delete in the database tables?
tables: TestTable1, TestTable2, TestTable3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '1234',
'PORT': 3306,
'HOST': '127.0.0.1',
'OPTIONS': {
'charset': 'utf8mb4',
'use_unicode': True, },
}
}
You can use custom sql directly. Have a look at:
https://docs.djangoproject.com/en/3.2/topics/db/sql/#executing-custom-sql-directly

How to make tests with Django using real data?

Here is my situation:
We have a project that started by the wrong way, without testing. The Project uses Python 3.6* and Django 2.*. Anyway, now we are trying to code the tests and for this we are reading "Obey the Testing Goat" book, for Django tests.
The problem is that we are in a closed network and our system uses LDAP to make login. LDAP user is compared with our Workers database and, if the worker was not fired or in vacances, he can log in the system. For This Worker database (HR) we just have access to make queries, not inserts or updates, create, delete, etc... In resume, we have no power to manage LDAP and HR Database, just make queries. Other thing: we have no power (privileges) to create more than one database in our SQLServer. They created for us a TestDatabase. So, I always need to put --keepdb when I run the test command.
Also, if there is more than one database in settings, Django Tests will try to create both databases. As we have in settings.py the HR Database (it's not managed) and the Application Database (as default), Django are trying to create both databases. But the HR database is just to do queries! How to do? We can't create workers records, just use them.
In resume: can I test with real data, making queries from a real database instead the temporary database created by Django? Can you help me, please? Any idea?
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'MarcoMob_TEST',
'USER': 'MarcoMob_TESTuser',
'PASSWORD': 'a_strong_password',
'HOST': 'SOME_HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
'TEST': {
'NAME': 'MarcoMob_TEST',
}
},
'rh': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'RH',
'HOST': 'SOME_HOST_FOR_HR',
'USER': 'some_user_for_hr',
'PASSWORD': 'some_password',
'PORT': 'port',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'MPInventoryDES',
'USER': 'MPInventorydesuser',
'PASSWORD': 'a_strong_password',
'HOST': 'SOME_HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
'rh': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'RH',
'HOST': 'SOME_HOST_FOR_HR',
'USER': 'some_user_for_hr',
'PASSWORD': 'some_password',
'PORT': 'port',
}
}

Django Oracle connection as SYS should be as SYSDBA or SYSOPER

I'm trying to use Oracle Database for Django. Oracle DB is active and I can connect with SQLDeveloper. But cannot connect from Django to Oracle DB.
I got this error:
django.db.utils.DatabaseError: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
I use Conntection Type = Basic, Role = SYSDBA in SQLDeveloper. Where to give this parameters in Django?
My Current setting.py parameters:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'payman',
'USER': 'sys',
'PASSWORD': 'ssys',
'HOST': '172.55.79.9',
'PORT': '1521',
}
}
You need to provide this in the 'OPTIONS' to cx_oracle. I set the following while using the full DSN mode:
'OPTIONS': {
'purity': cx_Oracle.ATTR_PURITY_SELF,
'mode': cx_Oracle.SYSDBA
},

Using existing database in Django

I'm using multiple databases in a Django app. The default database is the one Django creates for user authentication, etc. Then I have a vo database, with existing data, which I plan to use for the content generation.
I wanted the classes in models.py to link up to the existing tables in vo. However, when I do
manage.py syncdb --database=vo
a set of new empty tables are created. BTW, it's a SQLite3 database.
How does one link existing tables in a database to the classes on models.py in Django?
Many thanks.
You need to add your db vo to settings.
if you have your database settings like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(DIR, 'django.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
}
Add vo database settings to it like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(DIR, 'django.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
# this your existing db
'vo': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(DIR, 'vo.sqlite'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
}
Then you can generate models automatically from the database.
$ ./manage.py inspectdb --database=vo > your_app/models.py
Configure database routers.
Check out: Using Django's Multiple Database Support

Specify database to use in Django

I need to use multiple databases for my django project. The application works fine when there is only one database:
In setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
But if I added more databases from the same engine:
DATABASES = {
'default':{},
'mydb1': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb1',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
'mydb2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb2',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
it gives me following error:
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
Also, i tried:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb1',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
'mydb2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb2',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
It only sees mydb1, not mydb2, when i tried query mydb2, it gives me:
DoesNotExist: Site matching query does not exist.
Do I need to define database route? it seems that I only need to do that for customized read/write.
Thanks
UPDATE:
In django docs, it says "The default routing scheme ensures that if a database isn't specified, all queries fall back to the default database".
So I guess my actual question is how do I specify a database to use for my queries?
It is explicetely stated in docs
The DATABASES setting must configure a default database; any number of
additional databases may also be specified.
If the concept of a default database doesn’t make sense in the context
of your project, you need to be careful to always specify the database
that you want to use.
As in your second example default database is not configured
DATABASES = {
'default':{},
...
}
when you access your data with no database specified, a django.db.backends.dummy backend is used, which complains on your configuration with ImproperlyConfigured error.
An example of configuring multiple database usage with Database Routers can be found in docs
update
Site matching query error is for completely different reasons, and is another question. Answer here, as it is duplicate of many others: as your mysql1 and mysql2 dbs have different content, second one seems to not to be properly configured. Refer site matching query does not exist.

Categories