温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在django 2.2中使用mysql

发布时间:2021-03-24 16:28:54 来源:亿速云 阅读:131 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关怎么在django 2.2中使用mysql,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

第一个坑(提示你的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

以上就是怎么在django 2.2中使用mysql,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI