首页 > 解决方案 > Django - 数据库设计

问题描述

到目前为止,我有以下数据库:

Person model
Student model inherits from Person, no added functionality
Lecturer model same as Student
Course model 
     leader = ForeignKey to Lecturer (1 Lecturer can have many courses)
     students = ManytoMany with Student (Many students can take many courses)
Card model
     student = OneToOne with student (1 card per 1 student).
Event model
     course = ForeignKey to Course (One course can have many events)

现在我的问题是;我想根据以下标准标记参加活动的学生。在创建事件时,标记的学生需要为空。稍后我将创建一个注册卡 ID 的视图。卡片模型与学生是一对一的关系。学生是多对多的课程。课程是对事件的 FK。

我该怎么做呢?

标签: pythondjangodatabase

解决方案


如果是我,我可能会使用专用的出勤表(如下所示):

class Attendance(Model):
    student = models.ForeignKey('Student')
    event = models.ForeignKey('Event')
    present = models.BooleanField(default=True)

如果这是您想要的,您可以将学生字段换成卡片(但由于它是一对一的,所以这并不重要)。字段的默认值present也可以交换,具体取决于您希望如何处理此表中的条目。


推荐阅读