在Django应用程序中获取所有表名(Get all table names in a Django app)
如何获取Django应用程序中的所有表名?
我使用下面的代码,但它没有得到由ManyToManyField创建的表
from django.db.models import get_app, get_models app = get_app(app_name) for model in get_models(app): print model._meta.db_tableHow to get all table names in a Django app?
I use the following code but it doesn't get the tables created by ManyToManyField
from django.db.models import get_app, get_models app = get_app(app_name) for model in get_models(app): print model._meta.db_table最满意答案
from django.db import connection tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables)正如在manage.py的syncdb命令中所见。
在下面的评论中,在上述答案的几年后, ThePhi说(我没有测试过):
from django.apps import apps from django.contrib import admin from django.contrib.admin.sites import AlreadyRegistered app_models = apps.get_app_config('my_app').get_models() from django.db import connection tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables)As seen in the syncdb command for manage.py.
In a comment below, years after the answer above, ThePhi says (I haven't tested it):
from django.apps import apps from django.contrib import admin from django.contrib.admin.sites import AlreadyRegistered app_models = apps.get_app_config('my_app').get_models()更多推荐
发布评论