python 如何从 Django 中的 sql 模式生成数据模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2249489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:10:55  来源:igfitidea点击:

How to generate data model from sql schema in Django?

pythondatabasedjango

提问by LNK2019

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.

我们的网站使用 PHP 前端和 PostgreSQL 数据库。除了 phpPgAdmin,我们目前没有后端。数据库管理员必须手动将数据输入到 phpPgAmin 中,这容易出错且繁琐。我们想使用 Django 来构建后端。

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

数据库已经有几十个表。是否可以将数据库模式导入 Django 并自动创建模型?

回答by TM.

Yes it is possible, using the inspectdbcommand:

是的,可以使用inspectdb命令:

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.pyand outputs model classes to standard output.

这将查看您配置的数据库settings.py并将模型类输出到标准输出。

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

正如 Ignacio 所指出的,文档中有针对您情况指南

回答by Ignacio Vazquez-Abrams

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

如果每个表都有一个自动增量整数 PK,那么您可以使用旧数据库指令。