How to get all table names in a Django app?
如何在Django应用程序中获取所有表名?
I use the following code but it doesn't get the tables created by ManyToManyField
我使用了下面的代码,但是它没有获得由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
3 个解决方案
#1
57
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.
如在管理.py的syncdb命令中所见。
In a comment below, years after the answer above, ThePhi says (I haven't tested it):
在下面的评论中,多年后的答案,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()
#2
9
The simplest answer, that still allows you to pick which app you want, is to modify your code with one extra argument "include_auto_created".
最简单的答案是,使用一个额外的参数“include_auto_created”来修改代码。
from django.db.models import get_app, get_models
app = get_app(app_name)
for model in get_models(app, include_auto_created=True):
print model._meta.db_table
Obviously I got this by following celope's advice to read the syncdb source, so thanks for that - just documenting an exact answer that includes the app name as it was what I wanted and possibly others too in future.
显然,我是根据celope的建议来阅读syncdb源代码的,所以谢谢你——只是记录一个准确的答案,其中包含了我想要的应用程序名,而且将来可能也包括其他的。
#3
0
The simplest method should be to use the database reflection APIs in django to go directly to the database. The drawback to this is that it won't give you any tables before you've synced.
最简单的方法应该是使用django中的数据库反射api直接访问数据库。这样做的缺点是在同步之前它不会提供任何表。
#1
57
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.
如在管理.py的syncdb命令中所见。
In a comment below, years after the answer above, ThePhi says (I haven't tested it):
在下面的评论中,多年后的答案,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()
#2
9
The simplest answer, that still allows you to pick which app you want, is to modify your code with one extra argument "include_auto_created".
最简单的答案是,使用一个额外的参数“include_auto_created”来修改代码。
from django.db.models import get_app, get_models
app = get_app(app_name)
for model in get_models(app, include_auto_created=True):
print model._meta.db_table
Obviously I got this by following celope's advice to read the syncdb source, so thanks for that - just documenting an exact answer that includes the app name as it was what I wanted and possibly others too in future.
显然,我是根据celope的建议来阅读syncdb源代码的,所以谢谢你——只是记录一个准确的答案,其中包含了我想要的应用程序名,而且将来可能也包括其他的。
#3
0
The simplest method should be to use the database reflection APIs in django to go directly to the database. The drawback to this is that it won't give you any tables before you've synced.
最简单的方法应该是使用django中的数据库反射api直接访问数据库。这样做的缺点是在同步之前它不会提供任何表。