django 2.2和mysql使用的常见问题


Posted in Python onJuly 18, 2019

可能是由于Django使用的MySQLdb库对Python3不支持,我们用采用了PyMySQL库来代替,导致出现各种坑,特别是执行以下2条命令的是时候:

python manage.py makemigrations
or
python manage.py inspectdb

第一个坑(提示你的mysqlclient版本过低)

无聊你是否执行pip install mysqlclient安装的最新版的,都抛出:

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None

MD,LZ看到这错误太想骂人了,没办法采取网上的方法,注释大法!

找到Python安装路劲下的Python36-32\Lib\site-packages\django\db\backends\mysql\base.py文件

将文件中的如下代码注释(可能需先关闭pycharm IDE)

if version < (1, 3, 3):
  raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

第二个坑(str类型没有decode方法)

对对对,py3默认str是unicode编码,通过encode方法编码成bytes类型,后者才有decode解码方法。
提示错误来源:Python36\lib\site-packages\django\db\backends\mysql\operations.py", line 149, in last_executed_query

这里网上一搜一堆的把encode改成decode方法,我靠,这谁的脑洞无敌了
源方法内容(pip安装的django 2.2.1原封不动的内容):

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.
    query = getattr(cursor, '_executed', None)
    if query is not None:
      query = query.decode(errors='replace')
    return query

通过print大法输出query结果,内容为

SELECT @@SQL_AUTO_IS_NULL

数据类型为str

这里网上还有注释大法,LZ不知道注释了if的后遗症是啥有没有影响,于是也没采纳。

于是我去django的github去翻这个文件这个方法的最新/历史版本,结果最新master分支内容如下:

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')

看函数名,应该是强制去把SQL转换成str了

我靠!!!这尼玛官网2.2.1/2.2.2(当前最新版)的包不是害人么,记得该文件上面引入下这个方法

from django.utils.encoding import force_str

然后再执行managa.py命令,可以了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用循环实现批量创建文件夹示例
Mar 25 Python
Python实现生成简单的Makefile文件代码示例
Mar 10 Python
Python单元测试框架unittest使用方法讲解
Apr 13 Python
python开启多个子进程并行运行的方法
Apr 18 Python
Python序列操作之进阶篇
Dec 08 Python
python利用MethodType绑定方法到类示例代码
Aug 27 Python
python操作excel让工作自动化
Aug 09 Python
利用Python脚本实现自动刷网课
Feb 03 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
Feb 07 Python
python DES加密与解密及hex输出和bs64格式输出的实现代码
Apr 13 Python
Python 远程开关机的方法
Nov 18 Python
python的setattr函数实例用法
Dec 16 Python
详解Python二维数组与三维数组切片的方法
Jul 18 #Python
Django框架视图介绍与使用详解
Jul 18 #Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
Jul 18 #Python
使用django实现一个代码发布系统
Jul 18 #Python
python 将字符串中的数字相加求和的实现
Jul 18 #Python
TensorFlow实现简单的CNN的方法
Jul 18 #Python
windows上安装python3教程以及环境变量配置详解
Jul 18 #Python
You might like
PHP安全配置
2006/10/09 PHP
ThinkPHP中RBAC类的四种用法分析
2014/11/24 PHP
Zend Framework教程之资源(Resources)用法实例详解
2016/03/14 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
js版本A*寻路算法
2006/12/22 Javascript
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
js浮点数保留两位小数点示例代码(四舍五入)
2013/12/26 Javascript
JavaScript异步编程Promise模式的6个特性
2014/04/03 Javascript
jquery+css3打造一款ajax分页插件(自写)
2014/06/18 Javascript
jquery实现仿Flash的横向滑动菜单效果代码
2015/09/17 Javascript
jQuery、zepto、js常用小技巧
2017/02/12 Javascript
jQuery轻松实现无缝轮播效果
2017/03/22 jQuery
详解微信小程序Page中data数据操作和函数调用
2017/09/27 Javascript
关于express与koa的使用对比详解
2018/01/25 Javascript
React组件中的this的具体使用
2018/02/28 Javascript
原生JS无缝滑动轮播图
2019/10/22 Javascript
JQuery常用选择器功能与用法实例分析
2019/12/23 jQuery
JavaScript实现轮播图特效
2020/04/10 Javascript
python字符串对其居中显示的方法
2015/07/11 Python
python爬取拉勾网职位数据的方法
2018/01/24 Python
关于python写入文件自动换行的问题
2018/06/23 Python
Python合并多个Excel数据的方法
2018/07/16 Python
Python Requests库基本用法示例
2018/08/20 Python
Python接口自动化判断元素原理解析
2020/02/24 Python
python打开音乐文件的实例方法
2020/07/21 Python
美国婴儿用品及配件购买网站:Munchkin
2019/04/03 全球购物
如何开启linux的ssh服务
2015/02/14 面试题
委托公证书
2014/04/08 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
国际商务专业毕业生自我鉴定2014
2014/09/27 职场文书
破坏寝室公物检讨书
2014/11/17 职场文书
贫困生证明范文
2015/06/16 职场文书
民主生活会主持词
2015/07/01 职场文书
react合成事件与原生事件的相关理解
2021/05/13 Javascript
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
2021/06/22 Python
每日六道java新手入门面试题,通往自由的道路
2021/06/30 Java/Android