How to generate data model from sql schema in Django?

Question:

Our website uses a PHP front-end and a PostgreSQL database. We don’t have a back-end at the moment except phpPgAdmin. The database admin has to type data into phpPgAmin manually, which is error-prone and tedious. We want to use Django to build a back-end.

The database has a few dozen of tables already there. Is it possible to import the database schema into Django and create models automatically?

Asked By: LNK2019

||

Answers:

If each table has an autoincrement integer PK then you can use the legacy database instructions.

Yes it is possible, using the inspectdb command:

python manage.py inspectdb

or

python manage.py inspectdb > models.py

to get them in into the file

This will look at the database configured in your settings.py and outputs model classes to standard output.

As Ignacio pointed out, there is a guide for your situation in the documentation.

Answered By: TM.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.