Django2.2报错::AttributeError: ''str'' object has no attribute ''decode''
程序员文章站
2024-03-25 11:33:10
...
遇到的错误:
AttributeError: ‘‘str’’ object has no attribute '‘decode’'
这基本是使用Django2.2的童鞋们经常遇到的问题!!!
下面给出 三种解决方法。
首先需要说明的是:
这三种解决方法时临时解决方法。
其次:
若以后童鞋们遇到更好的解决方法,可以在下方评论。
方法一:
这个最简单粗暴!
直接将出错的两句代码给注释掉。
出错代码位于
/home/soul/anaconda3/lib/python3.6/site-packages/django/db/backends/mysql/operations.py
第140行
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
方法二:
这个方案看起来有些多余,对比 django 1.11版本代码以及代码段注释,这段代码加入的具体意义有些莫名其妙
但是为了保险起见,可以加入 try 方法达到另一种注释的方法
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)
try:
if query is not None:
query = query.decode(errors='replace')
return query
except:
return query
方法三:(推荐使用)
开源社区中,在最新的代码中, last_executed_query 函数被修改成如下:
# 19年11月27日,位于157行
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')
force_text 方法定义在 django/django/utils/encoding.py 的50行,对比代码发现force_text和force_str代码相同
这样就简单多了!!!
尝试修改代码
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
return force_text(query, errors='replace')
运行测试:
(base) [aaa@qq.com one_orm]$ python manage.py makemigrations
No changes detected
欧克了!!!
至此:问题解决!举手啦,鼓掌鼓掌。
上一篇: Android如何使用OKHttp
下一篇: OKHTTP的简单封装
推荐阅读
-
Django 报错:‘AttributeError: ‘str‘ object has no attribute ‘decode‘‘
-
【已解决】python-pip升级报错- AttributeError: 'NoneType' object has no attribute 'bytes'
-
AttributeError: ‘str‘ object has no attribute ‘decode‘ Python3
-
Django2.2报错::AttributeError: ''str'' object has no attribute ''decode''
-
python编程排除163邮箱发送邮件报错(AttributeError: ‘tuple‘ object has no attribute ‘encode‘)
-
python编程排除163邮箱发送邮件报错(AttributeError: ‘tuple‘ object has no attribute ‘encode‘)
-
解决Keras报错AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
-
str‘ object has no attribute ‘decode‘解决办法
-
使用torchsummary时报错AttributeError: ‘list‘ object has no attribute ‘size‘
-
python报错:AttributeError: 'module' object has no attribute 'xfeatures2d'