Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 324
liamlee Avatar asked Nov 27 '14 05:11

liamlee


3 Answers

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

python manage.py inspectdb --database=[dbname] [table_name] > output.py
like image 109
Mirza Arslan Baig Avatar answered Nov 02 '22 10:11

Mirza Arslan Baig


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

like image 37
Der Cold Bold Avatar answered Nov 02 '22 09:11

Der Cold Bold


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

like image 35
madzohan Avatar answered Nov 02 '22 09:11

madzohan