如何从Django中的SQL模式生成数据模型? [英] How to generate data model from sql schema in Django?

查看:159
本文介绍了如何从Django中的SQL模式生成数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网站使用PHP前端和PostgreSQL数据库。除了phpPgAdmin,我们目前没有后端。数据库管理员必须手动将数据键入phpPgAmin,这容易出错且乏味。我们要使用Django构建后端。

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.

数据库已经有几十个表了。可以将数据库模式导入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?

推荐答案

是的,可以使用 inspectdb 命令:

python manage.py inspectdb

python manage.py inspectdb > models.py

将其放入文件中

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

This will look at the database configured in your settings.py and outputs model classes to standard output.

正如Ignacio所指出的,有一个指南您的情况在文档中。

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

这篇关于如何从Django中的SQL模式生成数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆