zoukankan      html  css  js  c++  java
  • 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或更早)

  • 相关阅读:
    快速排序算法图文详解(模版使用)
    数据库中事务和隔离级别解析
    详解对象的创建,布局,定位,存活判断
    SpringCloud-Ribbon:认识并配置负载均衡
    SpringCloud-Feign:接口式微服务调用
    SpringCloud-Eureka(3)集群配置和了解CAP原则
    SpringCloud-Eureka(2)服务注册与信息配置
    SpringCloud-Eureka(1)认识Eureka和初步配置
    Promise
    es6
  • 原文地址:https://www.cnblogs.com/jrri/p/11610056.html
Copyright © 2011-2022 走看看