开发一个网站,网站的内容被划分为不同Course,每一个Course下有一些Lesson,目前利用Django自带的Admin来添加Lesson。
Course和Lesson在数据库中有相应的表,并且Lesson有一个ForeignKey是Course。现在在Course表中有一个total_lesson的属性记录这个Course下有多少课程,想要实现在Admin中增加一节Lesson就让Course的total_lesson属性同时+1,应该怎么更改views中的函数?
数据库定义如下:
class Course(models.Model):
course_name = models.CharField(max_length=255)
brief = models.TextField()
overview = models.TextField()
classification = models.CharField(max_length=32)
release_date = models.DateTimeField(auto_now_add=True)
course_auth = models.CharField(max_length=255, default='admin')
total_lesson = models.IntegerField(default=0)
def __str__(self):
return self.course_name
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
lesson_name = models.CharField(max_length=255)
lesson_num = models.IntegerField()
learn = models.TextField()
instructions = models.TextField()
hint = models.TextField()
language = models.CharField(max_length=32)
time_limit = models.CharField(max_length=16)
memory_limit = models.CharField(max_length=16)
def __str__(self):
return str(self.lesson_name)