django2.2以上使用pymsql报错问题解决方法

问题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.

解决方法:

  • 方法一:按要求安装mysqlclient 1.3.13或以上版本。
  • 方法二: 找到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'

解决方法:

  1. 找到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
  1. 修改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')
  1. 然后再执行makemigrations命令就可以了

你可能感兴趣的:(django2.2以上使用pymsql报错问题解决方法)