本文介绍了在Django应用中获取所有表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Django应用中获取所有表名?
How to get all table names in a Django app?
我使用以下代码,但没有得到ManyToManyField创建的表
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命令中看到.
As seen in the syncdb command for manage.py.
在上面的答案问世多年之后,在下面的评论中, ThePhi 说(我没有测试过):
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()
这篇关于在Django应用中获取所有表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!