0

In a Django project, executing manage.py sql APPNAME prints out the schema of the models.

But how can this be done when the models and views have been separated into several *_models.py and *_views.py?

I'm developing a flat, one page application that relies quite a bit on javascript for event manipulation and handling. To facilitate separation of concerns, I split out the model and view files into auth_*, dashboard_* and taxonomy_* files. How can I get the model schema for these files?

Jason
  • 11,263
  • 21
  • 87
  • 181
  • You should be using a REST framework and worrying about JSON, the whole point of the ORM is (almost) never having to think about SQL. Django Rest framework will give you finer grained access control, pagination and throttling as a bonus. – Paulo Scardine Aug 19 '13 at 01:25
  • Except when you have to worry about migrating to a new database schema from an existing one in circumstances that South doesn't cover. – Jason Aug 19 '13 at 01:27
  • At database migrations in circumstances that South doesn't cover, you probably will have to resort to your database shell, which already has provisions for listing and dumping structure of tables (or call a DBA). – Paulo Scardine Aug 19 '13 at 01:29
  • True, but it would be much more convenient to do this from the Django models. The existing DB has ~50+ tables, of which I'm using around 30. Going through the table names for a dump would be tedious, and I'm looking for an alternative – Jason Aug 19 '13 at 01:36

1 Answers1

1

Even if you split models into multiple files, you still need to create a 'models' package in your app, the project structure could be,

--app
----models
------__init__.py

in init.py,you need to import the model classes and set them into globals, you can do it manually or dynamically, e.g.,

from auth_* import XXXModel
current_global = globals()
current_global[XXXModel.__name__] = XXXModel

then, python manage.py sql can find the model schemas.

Qiang Jin
  • 4,427
  • 19
  • 16
  • You have an interesting solution, but I found this http://stackoverflow.com/a/18325412/214892 was a better option – Jason Aug 20 '13 at 18:43