如何解决这个"Invalid default value for a django.models field error"?

How to resolve this "Invalid default value for a django.models field error"?

我正在创建一个学生模型,用于记录学生的家庭作业详情。它的工作方式是让学生在几天或几小时的最后期限内在家完成工作。迁移时出现此错误我收到此错误

  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
    res = self._query(query)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
    db.query(q)
  File "C:\Users\Lenovo\Documents\GitHub\All-in-one-project\ven\lib\site-packages\MySQLdb\connections.py", line 254, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1067, "Invalid default value for 'deadline_type'")

对于我的这个模型

class WorkHour(models.Model):
    DAYS= 'D'
    HOURS= 'H'
    STATUS = [
        ('D', 'Days'),
        ('H', 'Hours')
    ]
    student= models.ForeignKey(Student, on_delete=models.CASCADE)
    date= models.DateField(verbose_name='Date')
    deadline_type= models.CharField(max_length=256,choices=STATUS,default= DAYS, verbose_name= "Days/Hours")
    deadline_time = models.IntegerField(verbose_name='Deadline',default=1)

要么我在deadline_type中添加default value,它给了我上面的错误。我也在我的其他模型中做了同样的事情。我不知道我在这里做错了什么。

您不能将 DAYS= 'D' 作为默认值,因为它不是元组。将字段更改为:

deadline_type= models.CharField(max_length=16, choices=STATUS, default=STATUS[0], verbose_name="Days/Hours")

这样您就可以删除 DAYS= 'D' HOURS= 'H' 并且 default 值将始终是 STATUS 的第一个元组。