【发布时间】:2014-11-27 05:32:08
【问题描述】:
我有一个包含 1000 个表的架构,其中很多我不需要, 我怎样才能只检查我需要的表?
【问题讨论】:
我有一个包含 1000 个表的架构,其中很多我不需要, 我怎样才能只检查我需要的表?
【问题讨论】:
你可以生成单个表的模型,运行这个命令
python manage.py inspectdb TableName > output.py
如果您想生成视图的模型,这也适用
【讨论】:
您可以在 python 控制台或 *.py 文件中执行此操作:
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
【讨论】:
在 Django 2.2 或更高版本中可以通过以下命令完成
python manage.py inspectdb --database=[dbname] [table_name] > output.py
【讨论】:
您可以通过以下方式获得所需表格的模型:
python manage.py inspectdb table1 table2 tableN > output.py
这样你可以只选择你想要的表格。
【讨论】:
您可以生成模型的 python 代码并以编程方式写入控制台。
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'
]
)
设置table=[]空列表获取所有表
【讨论】: