settings.py
// python 和mysql的连接入口,包含接入的数据库名、用户名、用户密码、数据库地址、数据库端口
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "test",
'USER':"root",
'PASSWORD':"******",
'HOST':"localhost",
'PORT':"3306",
}
}
//激活应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',//将创建的应用添加进去即可
]
myapp\models.py
//创建模型
from django.db import models
# Create your models here.
class test_tbl(models.Model):
name = models.CharField(max_length=20)
date = models.DateTimeField()
tem = models.IntegerField()
delete = models.BooleanField(default=False)
class test1_tbl(models.Model):
sname = models.CharField(max_length=20)
sdate = models.DateTimeField()
stem = models.IntegerField()
sdelete = models.BooleanField(default=False)
#关联test_tbl
stest = models.ForeignKey("test_tbl",on_delete=models.CASCADE)
mytets\__init__.py
//将数据库连接到python
import pymysql
pymysql.install_as_MySQLdb()
//数据库迁移命令
python manage.py makemigrations //生成迁移文件, models.ForeignKey()中需要加入on_delete=models.CASCADE
python manage.py migrate //数据迁移到数据库,注意用户的权限,报错后需要进入mysql将错误的
//django_content_type表删除