新创建django项目,配置mysql 报错:报错:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
配置:
settings.py配置默认数据库
"default":{
"ENGINE": "django.db.backends.mysql",
"NAME": "xx",
"USER": "root",
"PASSWORD": "xx",
"HOST": "localhost",
"PORT": "3306",
# 'OPTIONS': {
# # "init_command": "SET foreign_key_checks = 0;",
# "init_command": "SET FOREIGN_KEY_CHECKS=0;",
# }
},
__init__.py(和settings.py同一目录)
import pymysql
pymysql.install_as_MySQLdb()
执行数据迁移,报错:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决:
第一种:把django版本降低
第二种:根据报错提示
1. 找到django\db\backends\mysql安装的目录,我的环境是一个项目一个环境,即venv:
项目根路径\venv\Lib\site-packages\django\db\backends\mysql
2. 打开base.py文件,并找到以下代码
version = Database.version_info
在if中加入pass,并注释以下代码
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
如:
version = Database.version_info
if version < (1, 3, 13):
pass
"""
raise ImproperlyConfigured(
'mysqlclient 1.3.13 or newer is required; you have %s.'
% Database.__version__
)
"""
然后保存并关闭文件。
3. 打开 operations.py文件
找到以下代码:
query = query.decode(errors='replace')
将decode更改为encode,如下所示
query = query.encode(errors='replace')