Python Django:将日期保存到数据库中

Python Django:将日期保存到数据库中,python,django,Python,Django,我正试图将日期保存到考勤系统的数据库中。print语句工作正常,但是日期没有保存到数据库中。 请帮忙 models.py class Attendance(models.Model): last_here = models.DateTimeField() class Student(models.Model): student_name = models.CharField(max_length=200) student_gender = models.CharFie

我正试图将日期保存到考勤系统的数据库中。print语句工作正常,但是日期没有保存到数据库中。 请帮忙

models.py

class Attendance(models.Model):
    last_here = models.DateTimeField()


class Student(models.Model):
    student_name = models.CharField(max_length=200)
    student_gender = models.CharField(max_length=3, choices=GENDER, default='N/A')
    student_parent_email = models.EmailField()
    attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE, default=1)
views.py

while(num!=amount): #there are multiple students.
    stu = Student.objects.filter(squad='LearnToSwim1')[num]

    if request.POST.get(stu.student_name, '') == 'on':
        stu.attendance.last_here = datetime.today() #this does not work.

        print("Student attendance taken")
        print(stu.attendance.last_here) 

        stu.save()


    num += 1

您必须保存
考勤
模型,而不是
学生
模型ie
stu.attention.save()
即:


首先,您使用学生模型将数据保存在Attendance@c.grey是的,这两个模型是相关的。每个学生都有出勤。我在上面的问题中添加了学生模型。将foreginkey添加到出勤模型而不是学生模型
while(num!=amount): #there are multiple students.
    stu = Student.objects.filter(squad='LearnToSwim1')[num]

    if request.POST.get(stu.student_name, '') == 'on':
        stu.attendance.last_here = datetime.today() #this does not work.

        print("Student attendance taken")
        print(stu.attendance.last_here) 

        stu.attendance.save() # here


    num += 1