Django连接MySql数据库步骤

时间:2021-11-29 08:25:56
a. 先写类
from django.db import models

# cmdb_userinfo
class UserInfo(models.Model):
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)

b. 注册APP

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cmdb',
]
c. 执行命令
python manage.py makemigrations
python manage.py migrate

d. ********** 注意 ***********
Django默认使用MySQLdb模块链接MySQL
主动修改为pymysql,在project同名文件夹下的__init__文件中添加如下代码即可:
import pymysql
pymysql.install_as_MySQLdb()