Python3.6+Django2.2+mysql5.6数据库报错:AttributeError: 'str' object has no attribute 'decode'

1. 安装数据库驱动pymsql

$ pip3 install pymysql


2. 安装完毕,打开_init_.py,添加代码:

import pymysql 
pymysql.install_as_MySQLdb()

若有报错说什么版本问题的话。
解决方法:在python的MySQL包中,我的环境路径:F:\Project\PythonEnve\***\Lib\site-packages\django\db\backends\mysql下的 base.py 文件中,注释掉一下两行代码:
if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__) 

3. 我是想通过已有数据库生成模型。执行命令

python manage.py inspectdb userphone


报错:

 File "F:\Project\PythonEnve\***\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'

解决方法:

vi 上述文件,vim operations.py  注释掉if 和 下面结果,直接返回query数据,结果就可以正常生成了。

    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

不要看其他说什么把decode改成encode简直是搞笑!

你可能感兴趣的:(安装配置调错,Django开发)