django 2.2以上pymsqlエラー問題解決方法

2393 ワード

問題1
makemigrationsコマンドを実行するときのヒント:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.2.詳細は次のとおりです.
File "C:\Python37\lib\site-packages\django\db\backends\mysql\base.py", line 36, in 
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.2.


解決方法:
  • 方法1:mysqlclient 1.3.13以上のバージョンを必要に応じてインストールします.
  • 方法2:C:\Python37\lib\site-packages\django\db\backends\mysql\base.pyファイルを見つけ、以下のコード
    # if version < (1, 3, 13):
    #     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required;     you have %s.' % Database.__version__)
    
    
  • を注釈する.
    質問2:
    エラーメッセージAttributeError: 'str' object has no attribute 'decode'
    File "C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
        query = query.decode(errors='replace')
    AttributeError: 'str' object has no attribute 'decode'
    
    

    解決方法:
  • django\db\backends\mysql\operations.pyファイルの下にあるlast_を見つけました.executed_queryメソッド、以下のold
  •   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
    
  • last_を修正executed_queryメソッドnew
  • from django.utils.encoding import 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.
          # MySQLdb returns string, PyMySQL bytes.
          return force_str(getattr(cursor, '_executed', None), errors='replace')
    
  • それからmakemigrationsコマンドを実行すればいい