django - 在创建数据库模式方面需要帮助
问题描述
当前数据库模式 -
我有这 3 个表,Curriculum
、Assignment
和Student
. 每个都通过学生和课程之间的关系Student
分配一个。每个都
与使用 ForeginKey具有相同的关系。Curriculum
ForeginKey
Assignment
Curriculum
问题陈述Assignments
-每个大约有 100个Curriculum
,问题是,一些学生需要免除一些作业,所以我想要一种方法可以让学生免除作业 1、2 和 3,但让其余学生完成作业 1、2 和 3。
我失败的解决方案- 我尝试的是,创建一个ManyToManyField
与Student
表格相关的Assignment
表格。然而,对于每个学生来说,必须手动添加数百个作业会非常耗时。
class Curriculum(models.Model):
name = models.CharField(max_length=50, null=False)
subject = models.CharField(max_length=30, choices=SUBJECT)
grade_level = models.CharField(max_length=20, choices=CURRICULUMGRADE, null=False)
tracking = models.CharField(max_length=20, choices=TRACKING, null=False)
required = models.CharField(max_length=20, null=True)
recorded_from = models.CharField(max_length=20, choices=RECORDED, null=False)
semesterend = models.CharField(max_length=50, null=True)
username = models.CharField(max_length=50, null=True)
password = models.CharField(max_length=50, null=True)
loginurl = models.CharField(max_length=100, null=True)
weight = models.IntegerField(null=True)
level = models.CharField(max_length=20, choices=LEVEL, null=False)
class Student(models.Model):
epicenter_id = models.CharField(
null=False, blank=False, unique=True, max_length=10
)
last_name = models.CharField(null=False, max_length=50)
first_name = models.CharField(null=False, max_length=50)
email = models.EmailField(null=False, max_length=120)
phone_number = models.CharField(null=False, max_length=50)
additional_email = models.EmailField(max_length=120, null=True)
additional_phone_number = models.CharField(max_length=20, null=True)
grade = models.CharField(max_length=20, choices=GRADELEVEL, null=False)
curriculum = models.ForeginKey('curriculum', null=True, blank=True, on_delete=models.SET_NULL)
class Assignment(models.Model):
standard = models.ManyToManyField(
Standard)
curriculum = models.ForeignKey(
Curriculum, on_delete=models.CASCADE, related_name="curriculum_assignment"
)
name = models.CharField(max_length=500, null=False)
description = models.CharField(max_length=500, null=False)
status = models.CharField(max_length=30, choices=STATUS, null=False)
type_of = models.CharField(max_length=30, choices=TYPE, null=False)
解决方案
好的,所以我能想到的最佳解决方案是创建另一个表来存储所有豁免分配,称为ExemptAssignements
.
模型.py
class ExemptAssignments(models.Model):
student = models.ForeginKey('student', null=True, blank=True, on_delete=models.SET_NULL)
assignments = models.ManyToMany('assignment', null=True, blank=True)
现在,每当我想免除学生的任何作业时,我都可以通过管理面板手动选择他们,以及被免除的学生。
现在要获取所有作业的列表,不包括我已豁免的作业,我可以简单地使用。
student = Student.objects.first()
Assignment.objects.filter(curriculum=student.curriculum).exclude(id__in=[x.id for x i in ExemptAssignment.objects.filter(student=student).assignment.all()]
上面的查询只会显示没有被豁免的作业。
推荐阅读
- azure - 从 Azure 自动化帐户运行时,Get-AzureBlobContent 引发错误
- python - 如何将此代码循环 50 次并附加它生成的输出 json 并存储它?
- java - 如何从本机函数按值返回?
- laravel - 如何将外键插入我的数据库表
- sql - 如何使用 BCP 修复“[Microsoft][SQL Server Native Client 10.0]字符串数据,右截断”错误
- debian - i3wm:如何使用 urxvt 为终端设置真正的透明度?
- .net - 可以支持服务结构的 Microsoft Orleans 的发布日期是什么时候?
- qr-code - MRTK:如何从未与 HoloLens 配对的 Android 设备修复 SpectatorView?(二维码)
- sql - 比较 power bi 参数中的日期
- intellij-idea - 错误:在 IntelliJ 上创建 JavaFX 项目时找不到或加载主类 sample.Main