Adapt an existing database to a django app

陌路散爱 提交于 2021-02-04 13:22:06

问题


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.

  1. Design the correct data model, using Django.

  2. Map the correct model to whatever it is you have.

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!