Django 1.10.2 模型数据库操作
首先我的django 版本
>>> django.VERSION (1, 10, 2, u'final', 0)
setting.py:
DATABASES = { 'default': { 'ENGINE':'django.db.backends.mysql', 'NAME' : 'yyyhl', 'USER' : 'root', 'PASSWORD':'', 'HOST': '127.0.0.1', } }
模型表 models.py
from django.db import models class Item(models.Model): ItemId = models.AutoField(primary_key=True) ItemCode = models.CharField(max_length=50) ItemName = models.CharField(max_length=50) Remark = models.CharField(max_length=200) class Inventory (models.Model): InventoryId = models.AutoField(primary_key=True) Item = models.ForeignKey(Item, null=False) Amount = models.IntegerField(null=True) class InStockBill(models.Model): InStockBillId = models.AutoField(primary_key=True) InStockBillCode = models.CharField(max_length=40) InStockDate = models.DateTimeField(null=True) Operator = models.CharField(max_length=40) Item = models.ForeignKey(Item, null=False) Amount = models.IntegerField(null=True)
接下来检查
#我新建的模块名字是 app_one
#验证模型是否有效 命令:python manage.py check System check identified no issues (0 silenced). #用来检测数据库变更和生成数据库迁移文件 命令:python manage.py makemigrations 模块名字 #用来迁移数据库 python manage.py migrate # 用来把数据库迁移文件转换成数据库语言 python manage.py sqlmigrate app_one 0001
创建成功
查看数据库三个表创建成功: