lijiuchangxin 2019-11-09
app01 models.py
from django.db import models # Create your models here. class Book(models.Model): id=models.AutoField(primary_key=True) title=models.CharField(max_length=32,unique=True) pub_date=models.DateField() price=models.DecimalField(max_digits=8,decimal_places=2) publish=models.CharField(max_length=32)
settings.py
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘:‘orm‘, # 要连接的数据库,连接前需要创建好 ‘USER‘:‘ysl‘, # 连接数据库的用户名 ‘PASSWORD‘:‘123456‘, # 连接数据库的密码 ‘HOST‘:‘10.0.0.51‘, # 连接主机,默认本级 ‘PORT‘:3306 # 端口 默认3306 } }
因为django默认你导入的驱动是MySQLdb
python3 mysql的驱动是 pymysql
所以在工orm __init__.py 写入
import pymysql pymysql.install_as_MySQLdb()
settings.py 设置
LOGGING = { ‘version‘: 1, ‘disable_existing_loggers‘: False, ‘handlers‘: { ‘console‘:{ ‘level‘:‘DEBUG‘, ‘class‘:‘logging.StreamHandler‘, }, }, ‘loggers‘: { ‘django.db.backends‘: { ‘handlers‘: [‘console‘], ‘propagate‘: True, ‘level‘:‘DEBUG‘, }, } }
python manage.py makemigrations python manage.py migrate