django mysql\base.py 报错 KeyError: <class 'bytes'>`

 

追踪源码:

问题文件:Python36\lib\site-packages\django\db\backends\mysql\base.py

    def get_new_connection(self, conn_params):
        conn = Database.connect(**conn_params)
        conn.encoders[SafeText] = conn.encoders[six.text_type]
        conn.encoders[SafeBytes] = conn.encoders[bytes]
        return conn

 

解决方案

  1. 给MySQLclient降级。先卸载,再安装指定版本。pip3 uninstall mysqlclientpip3 install mysqlclient==1.3
  2. 我最开始的解决方案,改django的代码(Python36\lib\site-packages\django \db\backends\mysql\base.py),加一个if:
    def get_new_connection(self, conn_params):
        conn = Database.connect(**conn_params)
        conn.encoders[SafeText] = conn.encoders[six.text_type]
        # 先判断bytes是否存在于编码器中,如果在才执行操作
        if bytes in conn.encoders:
            conn.encoders[SafeBytes] = conn.encoders[bytes]
        return conn

 

posted @ 2022-12-01 18:04  子非鱼且于  阅读(1662)  评论(0编辑  收藏  举报
Live2D