如果一般的开发中,是先设计好数据库,然后再进行开发的, 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管理。