我正在 Django 中开发日历应用程序。
相关模型结构如下:
class Lesson(models.Model):
RECURRENCE_CHOICES = (
(0, 'None'),
(1, 'Daily'),
(7, 'Weekly'),
(14, 'Biweekly')
)
frequency = models.IntegerField(choices=RECURRENCE_CHOICES)
lessonTime = models.TimeField('Lesson Time')
startDate = models.DateField('Start Date')
endDate = models.DateField('End Date')
student = models.ForeignKey(Student)
class CancelledLesson(models.Model):
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
cancelledLessonDate = models.DateField() # Actual date lesson has been cancelled, this is startDate + Frequency
class PaidLesson(models.Model):
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
actualDate = models.DateField() # Actual date lesson took place
paidAmt = models.DecimalField('Amount Paid', max_digits=5, decimal_places=2)
paidDate = models.DateField('date paid')
class CompositeLesson(models.Model):
# only used to aggregate lessons for individual lesson management
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
actualDate = models.DateTimeField()
isCancelled = models.BooleanField()
canLesson = models.ForeignKey(CancelledLesson, blank=True, null=True)
payLesson = models.ForeignKey(PaidLesson, blank=True, null=True)
显然,这都会导致显示属于特定学生的课程出现问题。我正在尝试做的是显示一个表格,其中显示学生姓名以及所有预定课程的实例。我正在动态计算重复频率以避免炸毁我的数据库。重复的例外情况(即课程取消)存储在它们自己的表中。生成重复时,将根据取消的课程表检查重复。
在此处查看我的代码以生成重复(以及由此引起的问题的小目录):Can't get key to display in Django template
我对 Python 相对缺乏经验,并且正在使用这个项目来了解很多概念,所以如果我遗漏了一些本质上是“Pythonic”的东西,我深表歉意。