Get all table names in a Django app Get all table names in a Django app python python

Get all table names in a Django app


from django.db import connectiontables = 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()

Another user comments that this also works (also untested):

[ m._meta.db_table for c in apps.get_app_configs() for m in c.get_models() ]

And yet another recommends reading this answer:

Django get list of models in application


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".

from django.db.models import get_app, get_modelsapp = 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.


This works for

>>> python -m django version2.0.7

OK, here's how you do that:

>>> from django.apps import apps >>> polls_tables = apps.get_app_config("polls")>>> polls_tables.modelsOrderedDict([('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'])

you can also get field names of particular model

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

Further if you want you can get count of it by

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

To get all the values in database, you can do it as:

>>> list = [entry for entry in Entry.objects.values()]>>> for dict in list:...     print()...     for key in dict:...             print(key," : ", dict[key]) ... id  :  1blog_id  :  5headline  :  i have blog entry named DJANGOpub_date  :  2018-06-10n_comments  :  5n_pingbacks  :  1id  :  2blog_id  :  5headline  :  i have blog entry named DJANGOpub_date  :  2018-06-10n_comments  :  7