How do I inspectdb 1 table from database which Contains 1000 tables
Asked Answered
R

5

13

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?

Ridgeway answered 27/11, 2014 at 5:32 Comment(0)
E
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

Epochmaking answered 28/11, 2014 at 0:42 Comment(0)
T
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

Thicket answered 2/3, 2017 at 4:19 Comment(0)
E
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

Epochmaking answered 28/11, 2014 at 0:42 Comment(0)
S
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
Synopsis answered 31/8, 2020 at 11:9 Comment(0)
N
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.

Nuthouse answered 10/12, 2020 at 7:30 Comment(0)
H
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

Hydrophilous answered 9/8, 2020 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.