AttributeError: 'str' object has no attribute 'decode'' bytes' object has no attribute 'encode'

AttributeError:
‘str’ object has no attribute ‘decode’’ 或’bytes’ object has no attribute 'encode’

启动Django项目时可能会报错其中一种
遇到’str’ object has no attribute ‘decode’’ ,
需要把query = query.decode(errors=‘replace’)中decode改为encode。
遇到’bytes’ object has no attribute ‘encode’,需要把encode改为decode。
如果同时启动两个项目有可能造成冲突,不能同时启动。

解决方法:
修改文件源码:
当前python解释器安装目录/site-packages/django/db/backends/mysql/operations.py

第146行

将:
if query is not None:
    query = query.decode(errors='replace')
改为:
if query is not None:
    query = query.encode(errors='replace') if isinstance(query, str) else query.decode(errors='replace')

版权声明:本文为qq_38710789原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。