本文章主要介绍了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 <module> 
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命令就可以了

发布评论
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

记一次配置nfs两台linux之间文件共享讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。