views:

328

answers:

2

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
A: 

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.

boxed
+6  A: 
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.

celopes
Edited to include the mechanism to get only the installed apps models. I really recommend looking at the syncdb command code.
celopes