Django2.2報錯-AttributeError: ''str'' object has no attribute ''decode'...

安裝報錯類型,解決方案;python

1. 數據庫鏈接報錯

mysqldb只支持python2,pymysql支持3,都是使用c寫的驅動,性能更好mysql

# django中修改配置文件setting.py添加以下代碼:

import pymysql
pymysql.install_as_MySQLdb()

解決方案: 修改數據庫:mysqldb=>pymysqlsql

2. 由於切換數據庫致使版本錯誤
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

解決方案: 註釋掉檢測數據庫版本的代碼數據庫

# "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
 
# if version < (1, 3, 13):
#     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
3. 切換數據庫致使轉碼錯誤
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')

解決方案: ==暫時使用第二種類型==django

  1. 修改decode爲encode
  2. 把條件註釋掉,防止出現不可預知的錯誤,這個錯誤極可能是python2中類型str和unicode的緣由,python3中只有unicode類型數據
# "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146

 # if query is not None:
      #query = query.encode(errors='replace')

解決完成以後完美運行性能

相關文章
相關標籤/搜索