Django user.save() 在列 'is_superuser' 上失败不能为空
Django user.save() fails on Column 'is_superuser' cannot be null
我正在尝试使用此代码更新用户密码:
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): Column 'is_superuser' cannot be null
django.db.utils.IntegrityError: Column 'is_superuser' cannot be null
AttributeError: 'NoneType' object has no attribute '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'
Mysql 行来自 auth_user
:
| id | password | last_login | is_superuser | username | first_name | last_name | email | is_staff | is_active | date_joined |
+----+--------------------------------------------------------------------------------+----------------------------+--------------+----------+------------+-----------+-----------------------+----------+-----------+----------------------------+
| 16 | pbkdf2_sha2560000[=14=]gJWVPgCXeadjdfhdjslskfzurBD$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
后一切正常!
您可以通过在数据库 OPTIONS
:
中使用 'use_pure':True
来修复错误
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<set>',
'USER': '<set>',
'PASSWORD': '<set>',
'OPTIONS': {
'use_pure':True
},
}
}
感谢 Ciaran O'Sullivan,他找到了解决方法:https://bugs.mysql.com/bug.php?id=92001
该解决方案也适用于我。
P.S。 use_pure
意味着 mysql-connector
将使用纯 python 连接,而不是它的 C 扩展(我认为是错误所在)。
https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
P.P.S 该错误出现在版本 8.0.13 中,该版本添加了 python3.7 支持:https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-13.html
我正在尝试使用此代码更新用户密码:
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): Column 'is_superuser' cannot be null
django.db.utils.IntegrityError: Column 'is_superuser' cannot be null
AttributeError: 'NoneType' object has no attribute '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'
Mysql 行来自 auth_user
:
| id | password | last_login | is_superuser | username | first_name | last_name | email | is_staff | is_active | date_joined |
+----+--------------------------------------------------------------------------------+----------------------------+--------------+----------+------------+-----------+-----------------------+----------+-----------+----------------------------+
| 16 | pbkdf2_sha2560000[=14=]gJWVPgCXeadjdfhdjslskfzurBD$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
后一切正常!
您可以通过在数据库 OPTIONS
:
'use_pure':True
来修复错误
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<set>',
'USER': '<set>',
'PASSWORD': '<set>',
'OPTIONS': {
'use_pure':True
},
}
}
感谢 Ciaran O'Sullivan,他找到了解决方法:https://bugs.mysql.com/bug.php?id=92001
该解决方案也适用于我。
P.S。 use_pure
意味着 mysql-connector
将使用纯 python 连接,而不是它的 C 扩展(我认为是错误所在)。
https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
P.P.S 该错误出现在版本 8.0.13 中,该版本添加了 python3.7 支持:https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-13.html