python - 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。
- 他们必须是与活动相关联的同一课程的一部分。
- 稍后需要在模型中看到标记的当前学生。
我该怎么做呢?
解决方案
如果是我,我可能会使用专用的出勤表(如下所示):
class Attendance(Model):
student = models.ForeignKey('Student')
event = models.ForeignKey('Event')
present = models.BooleanField(default=True)
如果这是您想要的,您可以将学生字段换成卡片(但由于它是一对一的,所以这并不重要)。字段的默认值present
也可以交换,具体取决于您希望如何处理此表中的条目。
推荐阅读
- redirect - 发布和重定向到另一个页面不保存数据
- google-apps-script - 使用 Apps 脚本替换带有 \r 或 \n 的内容时出现问题
- docker - 当我使用绑定挂载时 Docker 容器退出(简单的 php/apache 设置)
- javascript - 如何在同一函数中使用异步数据两次更新状态
- react-native - react native 中钩子的正确语法
- java - restTemplate.postForObject 不发送对象的所有成员变量
- ios - 从上下文中删除后核心数据对象不为零
- android - Android Retrofit NetworkBoundResource 返回 null
- 2d - 如何在 2D 游戏(暗黑破坏神 II)中找到内存中的 XYZ 坐标
- python - Odoo 自定义过滤功能