Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

674
Visualizações
Access data from second external SQLite database in Django

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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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;
over 4 years ago · Santiago Trujillo Relatório

0

@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 migrate --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!!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda