准备将 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

最新文章

  1. CKEditor (Toolbar Definition)工具栏自定义配置
  2. find-all-anagrams-in-a-string
  3. poj 1039 Pipe(叉乘。。。)
  4. 安森美电量计采用内部电阻跟踪电流--电压HG-CVR
  5. jQuery 黑白插件
  6. 数位DP问题整理(一)
  7. 重装Ubuntu系统并配置开发环境
  8. Swift还是Objective-C
  9. Windows下Hadoop的环境安装[转]
  10. java变量的分类与初始化
  11. J-Link驱动下载和Hex程序下载
  12. Linux(CentOS6.5)下修改Nginx初始化配置
  13. Pytorch系列教程-使用Seq2Seq网络和注意力机制进行机器翻译
  14. face recognition[variations of softmax][ArcFace]
  15. NSFileManager和NSFileHandler的作用, category的理解
  16. C/C++掌握技能(三)
  17. css 的 conic-gradient 学习
  18. java中object数据怎么转换成json数据
  19. JavaWeb温习之HttpServletResponse对象
  20. css背景图片模糊

热门文章

  1. win7+vim搭建+verilog HDL IDE
  2. LDAP认证
  3. console调试技巧
  4. IT兄弟连 Java语法教程 流程控制语句 循环结构语句1
  5. 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
  6. DatabaseLibrary -数据库操作
  7. 315道Python常见面试题
  8. Vue中的组件及路由使用
  9. ECharts grid组件离容器的距离
  10. 【转载】Android 的 Handler 机制实现原理分析