如果使用python3环境,我们需要用pymysql代替mysqldb,在虚拟环境中使用下列代码安装pymysql。
pip install PyMySQL
在项目的 __init__.py中添加下列两行
import pymysql
pymysql.install_as_MySQLdb()
如果报错 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 则需要修改/lib/python3.6/site-packages/django/db/backends/mysql/base.py注释掉下列代码。
"""
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
"""
接下来可能会报错AttributeError: ‘str’ object has no attribute ‘decode’,我们需要修改lib/python3.6/site-packages/django/db/backends/mysql/operations.py文件以下代码
query = query.encode(errors='replace')