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
解决方案
- 给MySQLclient降级。先卸载,再安装指定版本。
pip3 uninstall mysqlclient
,pip3 install mysqlclient==1.3
。 - 我最开始的解决方案,改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