28

I basically want to take an existing mysql database structure created and used by a php app (codeigniter framework) and reverse engineer it to a django app. is there some tool to do this? south migrations maybe?

1 Answer 1

60

Create a project, and point your settings @ your database

Then run

./manage.py inspectdb

This will print out a python models file for the DB you're pointing at

You can output this to a file by doing something like

./manage.py inspectdb > models.py

And then you can move the file to the most suitable location, and edit it as needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. Chapter 18 of the Django Book has further details on using inspectdb to integrate existing databases - djangobook.com/en/2.0/chapter18
Great this is what i wanted exactly!, also going to generate the er diagram based on this using graphviz & django_extensions
Make sure you check the generated model.py carefully! In particular lengths of CharFields and missing ForeignKeys was the problem cases last time I used it. It saved many hours of making the model.py by hand though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.