django.db.utils.IntegrityError: (1048, "Column 'username' cannot be null")

求大神指教,这个怎么解决 报错信息如下: Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "E:\Person\DjangoProjects\day01\app01\views.py", line 20, in register userobj.save() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 746, in save force_update=force_update, update_fields=update_fields) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 784, in save_base force_update, using, update_fields, File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 887, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 926, in _do_insert using=using, raw=raw, File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1204, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1392, in execute_sql cursor.execute(sql, params) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 100, in execute return super().execute(sql, params) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\mysql\base.py", line 74, in execute return self.cursor.execute(query, args) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 517, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result result.read() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 1075, in read first_packet = self.connection._read_packet() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\connections.py", line 684, in _read_packet packet.check_error() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\protocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) django.db.utils.IntegrityError: (1048, "Column 'username' cannot be null") [13/Jul/2020 12:57:21] "GET /app01/register/ HTTP/1.1" 500 204770

评论 2

  • 这个错误通常意味着您正在尝试将一个具有空值的列存储到数据库中。在Django中,通常情况下,如果您的模型定义了一个CharField()TextField()字段,并且没有将其设置为null=True,则该字段不能为空。

    要解决这个问题,您可以采取以下步骤:

    1. 检查您的模型定义是否正确,并确保所有必填字段都已经设置了适当的默认值或允许为空。例如,在以下示例中,usernamepassword是必填字段,但email是可选的:

    class User(models.Model): username = models.CharField(max_length=255) password = models.CharField(max_length=255) email = models.EmailField(blank=True)

    1. 如果您已经有数据并且无法更改模型定义,请确保在数据迁移之前填充必填字段的值。您可以使用Django shell或编写脚本来实现这一点。

    2. 如果您使用了表单来收集用户输入,请确保您已经验证了所有必填字段。如果用户未填写这些字段,则应向他们显示错误消息并要求他们提供所需的信息。

    希望这可以帮助您解决问题。