sourcecode

1000개의 테이블이 포함된 데이터베이스에서 db 1 테이블을 검사하려면 어떻게 해야 합니까?

copyscript 2023. 8. 27. 09:45
반응형

1000개의 테이블이 포함된 데이터베이스에서 db 1 테이블을 검사하려면 어떻게 해야 합니까?

1000개의 테이블이 포함된 스키마를 얻었는데, 필요 없는 테이블이 많은데 어떻게 해야 필요한 테이블만 DB를 검사할 수 있습니까?

이 명령을 실행하여 단일 테이블의 모델을 생성할 수 있습니다.

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

이렇게 하면 원하는 테이블만 선택할 수 있습니다.

모델의 파이썬 코드를 생성하고 콘솔에 프로그래밍 방식으로 쓸 수 있습니다.

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=[]모든 테이블을 가져올 빈 목록

언급URL : https://stackoverflow.com/questions/27163702/how-do-i-inspectdb-1-table-from-database-which-contains-1000-tables

반응형