[英]Django user.save() fails on Column 'is_superuser' cannot be null
I am trying to update user password using this code:我正在尝试使用以下代码更新用户密码:
from django.contrib.auth.models import User
u = User.objects.get(username='test')
u.set_password('test')
u.save()
but it falis everytime on these errors (without long messages from whole error message):但每次出现这些错误时都会出错(没有来自整个错误消息的长消息):
_mysql_connector.MySQLInterfaceError: Column 'is_superuser' cannot be null
mysql.connector.errors.IntegrityError: 1048 (23000): Column 'is_superuser' cannot be null
django.db.utils.IntegrityError: Column 'is_superuser' cannot be null
AttributeError: 'NoneType' object has no attribute 'strip'
I am confused because this code is from official site so error is probably somewhere between django and mysql.我很困惑,因为这段代码来自官方网站,所以错误可能在 django 和 mysql 之间。
Thanks for advance感谢提前
EDIT:编辑:
Code and complete error message:代码和完整的错误信息:
>>> 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'
Mysql row from auth_user
:来自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 |
So it looks like this error is beacuse of mysql
engine mysql.connector.django
.所以看起来这个错误是因为mysql
引擎mysql.connector.django
。 After installing mysqlclient
using pip install mysqlclient
and changing ENGINE
inside settings.py
to django.db.backends.mysql
everything works well!安装完成后mysqlclient
使用pip install mysqlclient
,改变ENGINE
内settings.py
到django.db.backends.mysql
一切正常!
You can fix the bug by using 'use_pure':True
in the database OPTIONS
:您可以通过在数据库OPTIONS
使用'use_pure':True
来修复错误:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<set>',
'USER': '<set>',
'PASSWORD': '<set>',
'OPTIONS': {
'use_pure':True
},
}
}
Thanks to Ciaran O'Sullivan, who found the workaround: https://bugs.mysql.com/bug.php?id=92001感谢找到解决方法的 Ciaran O'Sullivan: https ://bugs.mysql.com/bug.php?id =92001
The solution also worked for me.该解决方案也对我有用。
PS use_pure
means that mysql-connector
will use pure python connection, instead of its C extension (where I believe the bug is). PS use_pure
意味着mysql-connector
将使用纯 python 连接,而不是它的 C 扩展(我认为是错误所在)。 https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
PPS The bug had appeared in version 8.0.13, which added python3.7 support: https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-13.html PPS 该bug出现在8.0.13版本,增加了对python3.7的支持: https ://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-13.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.