问题
I have a Postgresql databese with data. I want to create a django app with that database.
How can i import the tables to django models and/or views?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/1646786/adapt-an-existing-database-to-a-django-app