获取Django应用程序中的所有表名称

时间:2009-12-04 06:54:57

标签: python django many-to-many

如何在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

4 个答案:

答案 0 :(得分:60)

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

答案 1 :(得分:9)

最简单的答案,仍然允许您选择您想要的应用程序,是使用一个额外的参数“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

显然,我是按照celope的建议阅读syncdb源码得到的,所以非常感谢 - 只记录一个包含应用名称的确切答案,因为它是我想要的,也可能是将来的其他人。

答案 2 :(得分:0)

最简单的方法应该是使用django中的数据库反射API直接转到数据库。这样做的缺点是它在你同步之前不会给你任何表。

答案 3 :(得分:0)

这适用于

>>> python -m django version
2.0.7

好的,这是您的操作方式:

>>> from django.apps import apps 
>>> polls_tables = apps.get_app_config("polls")
>>> polls_tables.models
OrderedDict([('question', <class 'polls.models.Question'>), ('choice', <class 'polls.models.Choice'>), ('reporter', <class 'polls.models.Reporter'>), ('article', <class 'polls.models.Article'>), ('publication', <class 'polls.models.Publication'>), ('publicationarticle_publications', <class 'polls.models.PublicationArticle_publications'>), ('publicationarticle', <class 'polls.models.PublicationArticle'>), ('person', <class 'polls.models.Person'>), ('group', <class 'polls.models.Group'>), ('membership', <class 'polls.models.Membership'>), ('place', <class 'polls.models.Place'>), ('restaurant', <class 'polls.models.Restaurant'>), ('waiter', <class 'polls.models.Waiter'>), ('student', <class 'polls.models.Student'>), ('assembler', <class 'polls.models.Assembler'>), ('bike', <class 'polls.models.Bike'>), ('blog', <class 'polls.models.Blog'>), ('author', <class 'polls.models.Author'>), ('entry_authors', <class 'polls.models.Entry_authors'>), ('entry', <class 'polls.models.Entry'>), ('themeblog', <class 'polls.models.ThemeBlog'>), ('bookauthor', <class 'polls.models.BookAuthor'>), ('publisher', <class 'polls.models.Publisher'>), ('book_authors', <class 'polls.models.Book_authors'>), ('book', <class 'polls.models.Book'>), ('store_books', <class 'polls.models.Store_books'>), ('store', <class 'polls.models.Store'>), ('dummy', <class 'polls.models.Dummy'>)])
>>> polls_tables.models.keys()
odict_keys(['question', 'choice', 'reporter', 'article', 'publication', 'publicationarticle_publications', 'publicationarticle', 'person', 'group', 'membership', 'place', 'restaurant', 'waiter', 'student', 'assembler', 'bike', 'blog', 'author', 'entry_authors', 'entry', 'themeblog', 'bookauthor', 'publisher', 'book_authors', 'book', 'store_books', 'store', 'dummy'])

您还可以获取特定模型的字段名称

>>> questionTable = polls_tables.get_model("question")
>>> questionTable._meta.get_fields()
(<ManyToOneRel: polls.choice>, <django.db.models.fields.AutoField: id>, <django.db.models.fields.CharField: question_text>, <django.db.models.fields.DateTimeField: pub_date>)

如果您愿意的话,还可以通过

>>> len(questionTable._meta.get_fields())
4

要获取数据库中的所有值,可以按照以下步骤操作:

>>> list = [entry for entry in Entry.objects.values()]
>>> for dict in list:
...     print()
...     for key in dict:
...             print(key," : ", dict[key]) 
... 

id  :  1
blog_id  :  5
headline  :  i have blog entry named DJANGO
pub_date  :  2018-06-10
n_comments  :  5
n_pingbacks  :  1

id  :  2
blog_id  :  5
headline  :  i have blog entry named DJANGO
pub_date  :  2018-06-10
n_comments  :  7
相关问题