Adapt an existing database to a django app
I have a Postgresql databese with data. I want to create a django app with that database.
How can i import th开发者_StackOverflowe tables to django models and/or views?
There is a utility called manage.py inspectdb
to generate models from your existing database. It works pretty well.
$ python manage.py inspectdb > models.py
If your database is not very simple -- or very well designed -- you'll find it a poor fit with Django.
While the reverse engineering works well, you may find that the original database design was flawed and you have lots of clumsy workarounds.
The question is one of "legacy software" that works with the old data model.
I'd suggest you do the following.
Design the correct data model, using Django.
Map the correct model to whatever it is you have.
Write a conversion script that uses simple, direct SQL and the Django ORM to migrate data from non-Django-friendly to a better model.
If you have legacy software, you'll have to work out an appropriate data movement schedule.
If you don't have any legacy software, you'll run this conversion once.
精彩评论