Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: using more than one database with inspectdb?

My settings file's database section looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'C:/Users/Desktop/test.db'
    },
    'blah':{
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'C:/Users/Desktop/test2.db'
    }
}

When I run the command python manage.py inspectdb > models.py, I only get the model generated for the default database, but not the second one. How could I get both models generated?

like image 830
TheEyesHaveIt Avatar asked Jun 02 '16 02:06

TheEyesHaveIt


People also ask

Can you use multiple databases within the same Django project?

Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.

How do I use Inspectdb?

Django Inspectdb Refactor will automatically create the required folders and create separate python files for each model. You can install it via pip or to get the latest version clone this repo. Add ``inspectdb_refactor`` to your ``INSTALLED_APPS`` inside settings.py of your project. make models in that particular app.


2 Answers

From the documentation:

--database DATABASE

Specifies the database to introspect. Defaults to default.

So you can inspect your second database with:

python manage.py inspectdb --database blah

You cannot inspect both at the same time.

like image 165
solarissmoke Avatar answered Sep 16 '22 13:09

solarissmoke


You can specify a specific database like this:

python manage.py inspectdb --database=blah > you_app/models.py
like image 33
M.Void Avatar answered Sep 18 '22 13:09

M.Void