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.并且在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或更早)

转载于:https://www.cnblogs.com/jrri/p/11610056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值