Django2.2报错 AttributeError: 'str' object has no attribute 'decode'
2024-09-03 09:20:57
准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations
后报错: AttributeError: 'str' object has no attribute 'decode'
出现这个错误之后可以根据错误提示找到文件位置,打开 operations.py 文件,找到以下代码:
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
根据错误信息提示,说明 if 语句执行时出错, query 是 str 类型,而 decode()
是用来将 bytes 转换成 string 类型用的,(关于Python编码点这里),由于 query 不需要解码,所以直接将 if 语句注释掉就可以了
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
最新文章
- CKEditor (Toolbar Definition)工具栏自定义配置
- find-all-anagrams-in-a-string
- poj 1039 Pipe(叉乘。。。)
- 安森美电量计采用内部电阻跟踪电流--电压HG-CVR
- jQuery 黑白插件
- 数位DP问题整理(一)
- 重装Ubuntu系统并配置开发环境
- Swift还是Objective-C
- Windows下Hadoop的环境安装[转]
- java变量的分类与初始化
- J-Link驱动下载和Hex程序下载
- Linux(CentOS6.5)下修改Nginx初始化配置
- Pytorch系列教程-使用Seq2Seq网络和注意力机制进行机器翻译
- face recognition[variations of softmax][ArcFace]
- NSFileManager和NSFileHandler的作用, category的理解
- C/C++掌握技能(三)
- css 的 conic-gradient 学习
- java中object数据怎么转换成json数据
- JavaWeb温习之HttpServletResponse对象
- css背景图片模糊