在Django应用程序中获取所有表名(Get all table names in a Django app)

编程入门 行业动态 更新时间:2024-10-24 15:24:07
在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_table

How 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()

更多推荐

本文发布于:2023-08-06 10:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446694.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Django   table   app   names

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!