gpt4 book ai didi

python - Django user.save() 在列 'is_superuser' 上失败不能为空

转载 作者:行者123 更新时间:2023-11-29 05:48:09 25 4
gpt4 key购买 nike

我正在尝试使用此代码更新用户密码:

from django.contrib.auth.models import User
u = User.objects.get(username='test')
u.set_password('test')
u.save()

但它每次都因这些错误而失败(整个错误消息中没有长消息):

  • _mysql_connector.MySQLInterfaceError: Column 'is_superuser' cannot be null
  • mysql.connector.errors.IntegrityError: 1048 (23000): 列 'is_superuser' 不能为空
  • django.db.utils.IntegrityError: Column 'is_superuser' cannot be null
  • AttributeError: 'NoneType' 对象没有属性 'strip'

我很困惑,因为这段代码来自官方网站,所以错误可能在 django 和 mysql 之间。

感谢提前


编辑:

代码和完整的错误信息:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('jj', 'lennon@thebeatles.com', 'johnpassword')
>>> user.save()
>>> u = User.objects.get(username='jj')
>>> print(u.is_superuser)
None
>>> u.save()
Traceback (most recent call last):
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 395, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Column 'is_superuser' cannot be null

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/base.py", line 168, in _execute_wrapper
return method(query, args)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 398, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.IntegrityError: 1048 (23000): Column 'is_superuser' cannot be null

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/base.py", line 218, in execute
return self._execute_wrapper(self.cursor.execute, query, new_args)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/base.py", line 174, in _execute_wrapper
utils.IntegrityError(err.msg), sys.exc_info()[2])
File "home/mm/test/.env/lib/python3.7/site-packages/django/utils/six.py", line 683, in reraise
raise value.with_traceback(tb)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/base.py", line 168, in _execute_wrapper
return method(query, args)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 398, in cmd_query
sqlstate=exc.sqlstate)
django.db.utils.IntegrityError: Column 'is_superuser' cannot be null

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 606, in statement
return self._executed.strip().decode('utf8')
AttributeError: 'NoneType' object has no attribute 'strip'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "home/mm/test/.env/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 66, in save
super().save(*args, **kwargs)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/base.py", line 851, in _save_table
forced_update)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/base.py", line 900, in _do_update
return filtered._update(values) > 0
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/query.py", line 760, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1429, in execute_sql
cursor = super().execute_sql(result_type)
File "home/mm/test/.env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "home/mm/test/.env/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 "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/operations.py", line 127, in last_executed_query
return force_text(cursor.statement, errors='replace')
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/django/base.py", line 230, in __getattr__
return getattr(self.cursor, attr)
File "home/mm/test/.env/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 608, in statement
return self._executed.strip()
AttributeError: 'NoneType' object has no attribute 'strip'

来自 auth_user 的 Mysql 行:

| id | password                                                                       | last_login                 | is_superuser | username | first_name | last_name | email                 | is_staff | is_active | date_joined                |
+----+--------------------------------------------------------------------------------+----------------------------+--------------+----------+------------+-----------+-----------------------+----------+-----------+----------------------------+
| 16 | pbkdf2_sha256$150000$0gJWVPgCXeadjdfhdjslskfzurBD$yrWlrHM8foMnTm0MpDJ6yxv0/PI= | NULL | 0 | jj | | | lennon@thebeatles.com | 0 | 1 | 2019-08-12 21:43:49.021511 |

最佳答案

所以看起来这个错误是因为 mysql 引擎 mysql.connector.django。在使用 pip install mysqlclient 安装 mysqlclient 并将 settings.py 中的 ENGINE 更改为 django.db 之后。 backends.mysql 一切正常!

关于python - Django user.save() 在列 'is_superuser' 上失败不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57468313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com