How to read table data from non-default database in Django? - python

My django project uses 2 databases, 1 existing database and another one which I am creating through my project using Models. I have defined db's in settings.py as below :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'music': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'myMusicDb.sqlite3'),
}
}
I generated model data using inspectdb command and everything works fine if I remove the default db from settings.py and make the 'music' db the default db. My query is how to keep both db in settings.py and to work with both DataBases ? Is there any way to tell django to use specific database from settings.py ?

I found the answer after reading https://docs.djangoproject.com/en/1.7/topics/db/multi-db/.
We can either use routers or we can make use of using keyword in query such as -
Album.objects.using('music').all()
Thought to post it, may be it could help someone.

Related

Database config for Django testing

I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
I've checked user's permissions and I'm sure that this user have permission to create database.
My database config looks like this:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '****',
'PORT': '****',
}
}
My postgres db is on a server.
My questions are:
What is the right way to config my db and run tests?
Should I be using sqlite for testing?
If so how my code should look like, so I don't have to comment configs?
It looks like your DB user doesn't have permission to create a new database. Please, take a look here. This command-line utility allows you to create a user and set their permissions.
Example:
createuser my_user --createdb -W --username postgres
Note: you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.
Answering your questions:
You may have several configs for different stages, e.g development, testing, production.
You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.

Adding existing sqlite3 database to Django App

Trying to follow this tutorial: https://knivets.com/how-to-integrate-django-with-existing-database/
and this SO: Using existing database in Django
My settings.py databases setup:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
# existing db I wan't to add
'articles': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'articles.sqlite3'),
}
}
It does detect and generates a model based on the table I'm interested in. I've added this model to models.py and
python manage.py dbshell --database=articles
does show the table and that it holds data. However, in Django /admin the table shows up but breaks when I try to add to it and none of the existing data appears. This is the error that it throws:
OperationalError at /admin/core/articles/
no such table: articles
I appreciate any help ya'll can offer here.

Access Django Test Database

I have an API running on Heroku and would like to be able to test it using the test database. My problem I have is that the TestCase setUp(self) method adds the data to an automatically created test database. Then my tests send a POST request to the locally running version of itself. That code then is just using the regular default database instead of the test database the tests are running in.
Here's some code and what I've tried.
In my main settings.py I have named my database like so
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'theOneTrueDB',
}
}
And I read here (https://docs.djangoproject.com/en/3.0/topics/testing/overview/#the-test-database) that
The default test database names are created by prepending test_ to the value of each NAME in DATABASES.
So I figured in my views file for one of my apps I could just do
Tenant.objects.using('test_theOneTrueDB').all() and that would work but it says
django.db.utils.ConnectionDoesNotExist: The connection test_theOneTrueDB doesn't exist
I have also tried
Tenant.objects.using('test_default')
and many other things. I am lost. I would also be okay with setting up another database and then using that in the setup with
Tenant.objects.save(using='theOneTrueDBTest)
or something like that.
Any help would be appreciated!
EDIT:
I know have my settings looking like this
DATABASES = {
'default': {
'NAME': 'theOneTrueDB',
'ENGINE': 'django.db.backends.sqlite3',
},
'other': {
'NAME': 'theOneTrueTest',
'ENGINE': 'django.db.backends.sqlite3',
}
}
but then if I try to save to the other database like this
tempPM.save(using='other')
Then I get this error
AssertionError: Database queries to 'other' are not allowed in this test. Add 'other' to API.tests.TenantLogin.databases to ensure proper test isolation and silence this failure.
You don't need to access the test database directly. It will be used by default and you don't need to worry about it.
By default for sqlite3 backend will be used in-memory database, with the name like this file:memorydb_default?mode=memory&cache=shared,
you can examine the settings while tests are running via:
from django.conf import settings
print(settings.DATABASES)

How to connect django project to mysql database if the mysql database reside in wamp server?

I googgled alot and see many solutions, but none of them worked for me. I have a project in django and also an app as well. now, I want a connection to mysql database that reside in wamp server in order to interact (sending data or getting data) to database through my app.I also installed MYSQLDB for python. and I have done neccessary things in settings.py file.following is content of settings.py regradding of database
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'flask',
'USERNAME':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'',
}
}
In this link, you can find information about Django setup for WAMP. Also you may need to add port number to the database settings which is 3306 as default.

How to make two django projects share the same database

I need to make two separate Django projects share the same database. In project_1 I have models creating objects that I need to use in project_2 (mostly images).
The tree structure of project_1_2 is:
project_1/
manage.py
settings.py
project_1_app1/
...
...
project_2/
manage.py
settings.py
project_2_app1/
...
...
Which is the best approach?
EDIT: I'm using sqlite3 in my development environment.
I'd like to keep my two django projects as stand-alone projects (so that both can be upgraded safely from their respective repositories).
# in project_1/settings.py
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
..
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'development.db'),
},
}
...
# in project_2/settings.py
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
..
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'development.db'),
},
}
...
In this way, each project has its own development.db (the one that I need to be shared):
project_1/development.db
project_2/development.db
but I guess I need to do something more to make it shared (and unique).
The best for me would be to keep the development.db at project_1/ path and thus set the project_2/settings.py DATABASES to point to project_1/development.db.
You can simply define the same database in DATABASES in your settings.py. So, if your database is PostgreSQL, you could do something like this:
# in project_1/settings.py
DATABASES = {
'default': {
'NAME': 'common_db',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'project_1_user',
'PASSWORD': 'strong_password_1'
},
}
# in project_2/settings.py
DATABASES = {
'default': {
'NAME': 'common_db',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'project_2_user',
'PASSWORD': 'strong_password_2'
},
}
Note that both database users (project_1_user and project_2_user) should have the appropriate privileges on the database you wish to use. Or you could instead use the same user for both projects.
If you want to have more than just one database per project, you should take a look at the docs for multiple databases.
On another matter, since you share data, I guess you share functionalities as well. So for example, if project_1_app1 and project_2_app1 do same (or similar) things, it seems they could instead be a single reusable app.
Edit
Since you use sqlite3, you should ensure the path you use is the same. So, assuming that project_1 and project_2 are siblings, like so:
projects
project_1
settings.py
...
project_2
settings.py
...
you should try this:
# project_1/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, 'development.db'),
},
}
# project_2/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(
os.path.dirname(os.path.dirname(PROJECT_ROOT)),
'project_1',
'development.db'
),
},
}
This would give the structure you ask for. Note however that the projects are not both "standalone". project_2 is clearly dependent on project_1's database.
In any case, perhaps, you should also take a look at the os.path module for more info.
You just need to declare in your model in class meta the attribute db_table with a name diferent the name of app + model (Which are automatically generated by Django) the twice projects need the same models. before the run makemigrations and migrate.
class MyModel(models.Model):
class Meta:
db_table = 'MyModel'

Categories

Resources