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使用新浪微博api上传图片到微博示例
Jan 10 Python
用python读写excel的方法
Nov 18 Python
python版本坑:md5例子(python2与python3中md5区别)
Jun 20 Python
基于python时间处理方法(详解)
Aug 14 Python
Python递归实现汉诺塔算法示例
Mar 19 Python
Python生成短uuid的方法实例详解
May 29 Python
python爱心表白 每天都是浪漫七夕!
Aug 18 Python
Python查找数组中数值和下标相等的元素示例【二分查找】
Feb 13 Python
python循环定时中断执行某一段程序的实例
Jun 29 Python
python实现连连看辅助(图像识别)
Mar 25 Python
PyCharm2018 安装及破解方法实现步骤
Sep 09 Python
通俗讲解python 装饰器
Sep 07 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 Mysql类 可以参考学习熟悉下
2009/06/21 PHP
PHP把数字转成人民币大写的函数分享
2014/06/30 PHP
thinkPHP微信分享接口JSSDK用法实例
2017/07/07 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
Javascript生成json的函数代码(可以用php的json_decode解码)
2012/06/11 Javascript
基于jquery实现select选择框内容左右移动添加删除代码分享
2015/08/25 Javascript
jquery对dom节点的操作【推荐】
2016/04/15 Javascript
node 使用 async 控制并发的方法
2018/05/07 Javascript
详解Webstorm 下的Angular2.0开发之路(图文)
2018/12/06 Javascript
从零撸一个pc端vue的ui组件库( 计数器组件 )
2019/08/08 Javascript
layui 富文本赋值,取值,取纯文本值的实例
2019/09/18 Javascript
简要讲解Python编程中线程的创建与锁的使用
2016/02/28 Python
Python爬取三国演义的实现方法
2016/09/12 Python
python机器学习实战之K均值聚类
2017/12/20 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
2018/02/21 Python
Python Pandas批量读取csv文件到dataframe的方法
2018/10/08 Python
对Python 窗体(tkinter)树状数据(Treeview)详解
2018/10/11 Python
python实现QQ空间自动点赞功能
2019/04/09 Python
Python多叉树的构造及取出节点数据(treelib)的方法
2019/08/09 Python
ansible-playbook实现自动部署KVM及安装python3的详细教程
2020/05/11 Python
python中通过pip安装库文件时出现“EnvironmentError: [WinError 5] 拒绝访问”的问题及解决方案
2020/08/11 Python
PyChon中关于Jekins的详细安装(推荐)
2020/12/28 Python
Urban Outfitters美国官网:美国生活方式品牌
2016/08/26 全球购物
美国机场停车位预订:About Airport Parking
2018/03/26 全球购物
MUGLER官方网站:蒂埃里·穆勒香水
2019/11/26 全球购物
NOTINO英国:在线购买美容和香水
2020/02/25 全球购物
俄罗斯三星品牌商店:Samsungstore
2020/04/05 全球购物
秋季运动会加油稿200字
2014/01/11 职场文书
公司年会搞笑主持词
2014/03/24 职场文书
诚信考试标语
2014/06/24 职场文书
2014年内部审计工作总结
2014/12/09 职场文书
小学生学习保证书
2015/02/26 职场文书
2015年秋季运动会广播稿
2015/08/19 职场文书
《颐和园》教学反思
2016/02/19 职场文书
导游词之无锡唐城
2019/12/12 职场文书
OpenCV-Python直方图均衡化实现图像去雾
2021/06/07 Python