Django配置了MySQL数据库的连接之后,第一次连接之前,需要对MySQL数据库做【迁移 / 同步】操作。
该步骤不一定会成功。
比如,如题所示的错误,具体如下:

PS D:\PyCharm_data\django_me> .\venv\Scripts\python.exe manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\base.py", line 361, in execute
    self.check()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\management\commands\migrate.py", line 64, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\core\checks\database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
    return self._cursor()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
    self.ensure_connection()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
    self.init_connection_state()
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\base.py", line 231, in init_connection_state
    if self.features.is_sql_auto_is_null_enabled:
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\features.py", line 82, in is_sql_auto_is_null_enabled
    cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
  File "D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\utils.py", line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "D:\PyCharm_data\django_me\venv\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'
PS D:\PyCharm_data\django_me>

以上问题,比较容易发生在【Python 3.x】的版本中。
在Python 3.x中,上面错误中提到的文件【D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\operations.py】的【query】已经不需要再用【decode】解码,而是可以直接使用了。
因此,Python 3.x中,对【query】执行【decode】操作就出错了。

解法:
对文件【D:\PyCharm_data\django_me\venv\lib\site-packages\django\db\backends\mysql\operations.py】指定行做出修改。

备份原文件:

修改:

然后就没问题了:

PS D:\PyCharm_data\django_me> .\venv\Scripts\python.exe manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 09, 2019 - 13:40:01
Django version 2.2, using settings 'django_me.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

——————————————
Done。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒