django3 中使用 mysql

首先是修改 settings.py 文件

# 修改为 云数据库
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'market',
        'HOST': 'xxx.xxx.xxx.xxx',
        'PORT': 3306,
        'USER': 'xxx',
        'PASSWORD': 'xxxxxx'
    }
}

然后在 项目或者应用程序级别里面的 __init__.py 中添加

import pymysql

pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()

最后就是安装 pymysql 了。
使用命令 pip install pymysql

如果出现 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.0. 的错误
就使用 pip install mysqlclient==1.4.0

应该就可以了。

原文地址:https://www.cnblogs.com/gznb/p/13638913.html