[Fixed]-How do I inspectdb 1 table from database which Contains 1000 tables

10👍

You can do it in the python console, or in *.py file:

from django.core.management.commands.inspectdb import Command
from django.conf import settings
from your_project_dir.settings import DATABASES  #  replace `your_project_dir`

settings.configure()
settings.DATABASES = DATABASES

Command().execute(table_name_filter=lambda table_name: table_name in ('table_what_you_need_1', 'table_what_you_need_2', ), database='default')

https://github.com/django/django/blob/master/django/core/management/commands/inspectdb.py#L32

20👍

You can generate the model of a single table, running this command

python manage.py inspectdb TableName > output.py

This works also if you want to generate the model of a view

4👍

You can do it by the following command in Django 2.2 or above

python manage.py inspectdb --database=[dbname] [table_name] > output.py

1👍

You can get the models of the tables you want by doing:

python manage.py inspectdb table1 table2 tableN > output.py

This way you can select only the tables you want.

0👍

You can generate model’s python code and write to the console programmatically.

from django.core.management.commands.inspectdb import Command

command = Command()
command.execute(
    database='default',
    force_color=True,
    no_color=False,
    include_partitions=True,
    include_views=True,
    table=[
        'auth_group',
        'django_session'
    ]
)

set table=[] empty list to get all tables

Leave a comment