Django模型中的默认值

发布于 2025-02-08 12:10:24 字数 1158 浏览 1 评论 0原文

我创建了一个使用这些Feilds的模型。我想将start_dateEND_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2025-02-15 12:10:24

请检查之前检查迁移,我认为这是问题,
此外

start_date = models.DateField(default='2022-02-27')
end_date = models.DateField(default='2022-06-27')


添加django admin的出席率

Please check the migrations before, I think that is the issue,
moreover change the default date value to

start_date = models.DateField(default='2022-02-27')
end_date = models.DateField(default='2022-06-27')

then makemigrations and migrate, run the server, you will see something like this on the django admin:
Add Attendance in django admin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文