Django 2.2 + AttributeError:'str'对象没有属性'decode'

Posted

技术标签:

【中文标题】Django 2.2 + AttributeError:\'str\'对象没有属性\'decode\'【英文标题】:Django 2.2 + AttributeError: 'str' object has no attribute 'decode'Django 2.2 + AttributeError:'str'对象没有属性'decode' 【发布时间】:2021-07-05 12:21:35 【问题描述】:

我们已经完成Django1.11 升级到 2.2 版本,我们几乎完成了,但是当我们使用命令python manage.py makemigrations 然后我们得到以下错误。

但是当我们点击下面的链接时,问题就解决了。

Migrations error in django 2; AttributeError: 'str' object has no attribute 'decode'

但是,我只想知道有没有其他方法可以解决这个问题?我感觉会有要更新的库导致这个问题?

当我们按照上面链接的建议手动添加时,它工作正常,但是我们将 docker 集成到项目中,那么它会失败吗?

django 2.2 以下和 2.2.20 以上的版本不会发生同样的问题。

错误消息是:

(env4) user2@SK385 pro % python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/ddtrace/contrib/django/db.py", line 65, in cursor
    return DbApiTracedCursor(conn._datadog_original_cursor(), pin)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/base/base.py", line 256, in cursor
    return self._cursor()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/base/base.py", line 233, in _cursor
    self.ensure_connection()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
    self.connect()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.init_connection_state()
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 231, in init_connection_state
    if self.features.is_sql_auto_is_null_enabled:
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/user2/Desktop/env4/lib/python3.7/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 "/Users/user2/Desktop/env4/lib/python3.7/site-packages/ddtrace/contrib/dbapi/__init__.py", line 90, in execute
    return self._trace_method(self.__wrapped__.execute, self._self_datadog_name, query, , query, *args, **kwargs)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/ddtrace/contrib/dbapi/__init__.py", line 44, in _trace_method
    return method(*args, **kwargs)
  File "/Users/user2/Desktop/env4/lib/python3.7/site-packages/django/db/backends/utils.py", line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/Users/user2/Desktop/env4/lib/python3.7/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'
user2

【问题讨论】:

你用的是什么版本的mysqlclient和mysql? 问题已通过删除 PyMySQL==0.10.0 并添加 mysqlclient=2.0.2 版本解决 【参考方案1】:

我认为您遇到这个问题是因为您使用的是 PyMySQL。

该问题已在 ticket 30380 中修复,但由于 Django 不正式支持 PyMySQL,因此未向后移植到 Django 2.2.X。

一些选项是:

升级到 Django 3.0.X+ 从 PyMySQL 切换到 mysqlclient 修补您的 Django 2.2 版本(请参阅修复程序 here)

【讨论】:

删除了 PyMySQL==0.10.0 并添加了 mysqlclient=2.0.2 版本,它现在可以正常工作了

以上是关于Django 2.2 + AttributeError:'str'对象没有属性'decode'的主要内容,如果未能解决你的问题,请参考以下文章

Django 2.2 + AttributeError:'str'对象没有属性'decode'

UniqueConstraint 与 unique_together 之间的区别 - Django 2.2?

Django 2.2 默认登录网址

django-select2:如果没有选择国家,如何禁用城市选择? (django 2.2)

在 Django 2.2 中为 GRAPHENE 设置“SCHEMA”导入模块时出现问题

Python Django 2.2登录功能_2