Is it possible to generate django models from the database?(是否可以从数据库生成 django 模型?)
问题描述
我一直在家里摆弄 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 命令:
- http://docs.djangoproject.com/en/dev/ref/django-admin/#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 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以从数据库生成 django 模型?
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
