• Django2.X 数据库驱动PyMySQL兼容



    django2.2版本与 pymysql模块兼容出错, 错误代码:
    django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

    要求mysqlclient需要1.3.13版本及之后新版本

    其中
    mysqlclient 是python与mysql数据库链接的一个包, 由C语言编写.
    pymysql 是纯 python编写的与mysql数据链接的包.速度上可能稍逊一筹, 但胜在安装使用方便简洁.

    两包功能相似, 使用方法雷同.

    此处错误原因是因为django框架(2.2)默认使用链接的包是mysqlclient, 而不是pymysql.

    而一般我们使用pymysql的时候会在项目__init__.py文件中增加: pymysql.install_as_MySQLdb().让pymysql充当MySQLdb,而不是mysqlclient

    所以在此处才会提示mysqlclient的版本信息为pymysql的版本信息.

    Django框架在2.2中把MySQL使用force_str返回为str,导致此错误提示出现.
            
            Django2.2源码:

        def last_executed_query(self, cursor, sql, params):
            # With MySQLdb, cursor objects have an (undocumented) "_executed"
            # attribute where the exact query sent to the database is saved.
            # See MySQLdb/cursors.py in the source distribution.
            # MySQLdb returns string, PyMySQL bytes.
            return force_str(getattr(cursor, ‘_executed‘, None), errors=‘replace‘)


    解决办法三种:

    1. 使用mysql-client替代pymysql, 来连接python与mysql.

    2. 更改django源码, 注释掉对mysqlclient版本的判断,
    如出现错误代码,AttributeError: 'str' object has no attribute 'decode'.
    则在对应报错位置:
    .decode改为.encode
    或者

    导入

    from django.utils.encoding import force_str

    使用django自带的force_str来转化

    3. 当然, 还有回退也是一个解决此问题的方法, 回退Django框架至2.2之前(2.1.4或更早)

  • 相关阅读:
    Linux下文件的基本操作
    conpot_usage简要说明
    const声明常量以及特点
    let变量声明以及声明特性
    盒子模型
    文本样式
    行间距
    字体的其他样式
    字体分类
    字体样式
  • 原文地址:https://www.cnblogs.com/jrri/p/11610056.html
Copyright © 2020-2023  润新知