Get all table names in a Django app

前端 未结 4 2003
谎友^
谎友^ 2020-12-24 12:16

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          


        
4条回答
  •  悲哀的现实
    2020-12-24 13:03

    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_models
    app = 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.

提交回复
热议问题