Is it possible to create django models from a database?

I worked with Django and Django ORM at home, and I have to say that I consider this to be one of the best in terms of usability.

However, I was wondering if it can be used in "reverse".

Basically what I would like to do is to generate Django models from an existing database schema (from a project that doesn't use django and is pretty old).

Is it possible?

Update: This Oracle Database

+44
database oracle django orm
Jul 24 '09 at 18:49
source share
2 answers

Yes, use the inspectdb command:

inspectdb

It introspectively scans the database tables in the database pointed to by the DATABASE_NAME parameter and displays the Django model module (models.py file) to standard output.

Use this if you have an outdated database with which you want to use Django. The script will check the database and create a model for each table inside it.

As expected, the created models will have an attribute for each field in the table. Note that checkdb has a few special cases in the output of a field name:

[...]

+61
Jul 24 '09 at 18:52
source share

(Django 1.7.1) Simple execution of python manage.py inspectdb will create classes for all tables in the database and display on the console.

  $ python manage.py inspectdb 

Save this as a file using standard Unix output redirection:

  $ python manage.py inspectdb > models.py 

(This works for me with mysql and django 1.9)

+8
Jun 14 '16 at 18:40
source share



All Articles