This is my project structure
I am able to access the default SQLite database db.sqlite3
created by Django, by importing the models directly inside of my views files
Like - from basic.models import table1
Now, I have another Database called UTF.db
which is created by someone else, and I want to access it's data and perform normal QuerySet operations on the retrieved data
The problem is I don't know how to import the tables inside that database, as they are not inside any model file inside my project as it's created by someone else
I tried adding the tables inside the UTF.db
database to a models.py
file by first adding it to the settings.py
file like the following
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'otherdb':{
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'UTF.db',
}
}
And then using the inspectdb
command to add the tables to an existing models.py
file
The command I tried out -
python manage.py inspectdb > models.py
But, that just causes my models.py file to get emptied out
Does anyone know how this can be solved?
In the end, I wish to import the table data inside of my views files by importing the respective model
Copyright Notice:Content Author:「Mihir」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70150128/access-data-from-second-external-sqlite-database-in-django