是否可以从数据库生成Django模型? [英] Is it possible to generate django models from the database?
问题描述
我在家里一直在使用Django和Django ORM,我不得不说,就易用性而言,我认为它是最好的之一.
I've been messing around with Django and the Django ORM at home, and I've got to say, I feel it is one of the best out there in terms of ease of use.
但是,我想知道是否可以在反向"中使用它.
However, I was wondering if it was possible to use it in "reverse".
基本上我想做的是从现有数据库模式(从不使用django且非常老的项目中)生成Django模型.
Basically what I would like to do is generate Django models from an existing database schema (from a project that doesn't use django and is pretty old).
这可能吗?
更新:有问题的数据库是Oracle
Update: the database in question is Oracle
推荐答案
是的,请使用inspectdb
命令:
inspectdb
inspectdb
检查DATABASE_NAME设置指向的数据库中的数据库表,并将Django模型模块(models.py文件)输出到标准输出.
Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
如果您有要使用Django的旧数据库,请使用此选项.该脚本将检查数据库并为其中的每个表创建一个模型.
Use this if you have a legacy database with which you'd like to use Django. The script will inspect the database and create a model for each table within it.
如您所料,创建的模型将为表中的每个字段都具有一个属性.请注意,inspectdb在其字段名称输出中有一些特殊情况:
As you might expect, the created models will have an attribute for every field in the table. Note that inspectdb has a few special cases in its field-name output:
[...]
这篇关于是否可以从数据库生成Django模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!