Home > Blockchain >  Access data from second external SQLite database in Django
Access data from second external SQLite database in Django

Time:11-30

This is my project structure

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

CodePudding user response:

You can specify specific database as specified in Documentation

python manage.py inspectdb --database=otherdb > your_app/models.py

Also if possible putting otherdb in a different App is better.

CodePudding user response:

You can attach the second database to the first one and use it from within the first one. You can use tables from both databases in single sql query. Here is the doc https://www.sqlite.org/lang_attach.html.

attach database '/path/to/dbfile.sqlite' as db_remote;

select * 
from some_table
join db_remote.remote_table on ....

detach database db_remote;

CodePudding user response:

@sevdimali's answer works fine to create the new models from an existing database

Once that is done you need to use the command -

python manage.py makemigrations

To add the changes to the migrations folder

Then use the command

python manage.py --fake-initial

To add the new changes to your database

Note: Use --fake-initial to ignore the already created tables while adding your new tables (so that they don't get recreated)

If there are no pre-existing models you can use

python manage.py migrate

Just like you do it normally


Now while importing the Database model inside of your view you can write

from Appname.models import Tablename

Where Appname is the App in which you added your new models

And to access it's data

qs=Tablename.objects.using('otherdb').all()

Note: otherdb is the name of the external database specified in the settings.py file

Now you can access all of your data in a similar way!!

  • Related