Django模型中的默认值
我创建了一个使用这些Feilds的模型。我想将start_date
和END_DATE
设置为默认日期,但是每当注册出勤率时,当我迁移时,我会得到此错误
django.core.exceptions.validationerror:['“ 26/06/2022”值的日期格式无效。它必须采用yyyy-mm-dd格式。']
模型中的模型。
class Attendance(models.Model):
ATTENDANCE = (
('absent', 'Absent'),
('present', 'Present')
)
student_id = models.CharField(max_length=100, null=True)
course_id = models.CharField(max_length=100, null=True)
date = models.DateField(auto_now_add=True, null=True)
time = models.TimeField(auto_now_add=True, null=True)
attendanceState = models.CharField(max_length=20, null=True,choices=ATTENDANCE)
start_date = models.DateField(default=2022-2-27)
week_num = models.CharField(max_length=2, blank=True)
end_date = models.DateField(default=2022-6-27)
def __str__(self):
return '{}'.format(self.student_id)
def save(self, *args, **kwargs):
#print(self.start_date.isocalendar()[1])
if self.week_num == "":
self.week_num = self.start_date.isocalendar()[1]
super().save(*args, **kwargs)
I created a model with these feilds. I want to set the start_date
and end_date
to a default date whenever the attendance is registered however, when I migrate I get this error
django.core.exceptions.ValidationError: ['“26/06/2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
the model in model.py
class Attendance(models.Model):
ATTENDANCE = (
('absent', 'Absent'),
('present', 'Present')
)
student_id = models.CharField(max_length=100, null=True)
course_id = models.CharField(max_length=100, null=True)
date = models.DateField(auto_now_add=True, null=True)
time = models.TimeField(auto_now_add=True, null=True)
attendanceState = models.CharField(max_length=20, null=True,choices=ATTENDANCE)
start_date = models.DateField(default=2022-2-27)
week_num = models.CharField(max_length=2, blank=True)
end_date = models.DateField(default=2022-6-27)
def __str__(self):
return '{}'.format(self.student_id)
def save(self, *args, **kwargs):
#print(self.start_date.isocalendar()[1])
if self.week_num == "":
self.week_num = self.start_date.isocalendar()[1]
super().save(*args, **kwargs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请检查之前检查迁移,我认为这是问题,
此外
添加django admin的出席率
Please check the migrations before, I think that is the issue,
moreover change the default date value to
then makemigrations and migrate, run the server, you will see something like this on the django admin:
Add Attendance in django admin