Home > database >  Default value in django models
Default value in django models

Time:06-18

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)

CodePudding user response:

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

  • Related