I got a schema which Contains 1000 tables,and many of them I don't need, how can I just inspectdb the just tables that I need?
Asked
Active
Viewed 1.6k times
5 Answers
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

Shubho Shaha
- 1,869
- 1
- 16
- 22

Der Cold Bold
- 203
- 2
- 4
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

madzohan
- 11,488
- 9
- 40
- 67
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

Mirza Arslan Baig
- 166
- 6
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.

Devesh Pradhan
- 149
- 1
- 6
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

ozcanyarimdunya
- 2,324
- 1
- 18
- 21