Django的Web开发——Sql已存在数据库结构生成到Model的方法

如果一般的开发中,是先设计好数据库,然后再进行开发的, Django可以自动帮我们生产models.py

在这里插入图片描述
现在数据库中建好表结构
在终端中输入 python manage.py inspectdb
然后会显示下面所有的model语句,复制到models.py中

如果不想复制粘贴,可以删除指定的如App/models.py 中
终端中 python manage.py inspectdb > App/models.py

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.


# 这是通过python manage.py inspectdb 口令将数据库中已有的结构自动检测,然后生成models,在粘贴过来
from django.db import models


class Book(models.Model):
    b_name = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'book'

此时models.py 中的模型,都会带上 class Meta元信息
managed= False 即不受django的migrate管理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值