python3.7 django2.2 mysql 异常

混江龙づ霸主 提交于 2019-12-05 01:51:30

错误日志

mysqlclient 1.3.13 or newer is required;

File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

原因是使用了pymysql, pymysql PyMySQL==0.9.3 调试发现 django.db.backends.mysql.base.py 版本限制了1.3.13 以上; django 2.2.7 做了限制.

pymysql.init.py中替换了MySQLdb


VERSION = (0, 9, 3, None) #pymysql 库版本
if VERSION[3] is not None:
    VERSION_STRING = "%d.%d.%d_%s" % VERSION
else:
    VERSION_STRING = "%d.%d.%d" % VERSION[:3]


def get_client_info():  # for MySQLdb compatibility
    version = VERSION
    if VERSION[3] is None:
        version = VERSION[:3]
    return '.'.join(map(str, version))

connect = Connection = Connect

# we include a doctored version_info here for MySQLdb compatibility
version_info = (1, 3, 12, "final", 0) # 覆盖MySQLdb的版本, 与django2.2 版本冲突

NULL = "NULL"

__version__ = get_client_info()

def install_as_MySQLdb():
    """
    After this function is called, any application that imports MySQLdb or
    _mysql will unwittingly actually use pymysql.
    """
    sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"]

该问题的github issue 该问题在django orm层处理

该问题将在django3.0中处理

django3.0发布时间

因此,我建议在django3.0发布前,我们应该使用django2.0, 避免str/byte的问题,等3.0发布后,我们直接升级django到3.0即可。在此之前,我们的代码只要兼容django3.0就行。与pymysql无关。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!