【发布时间】:2020-02-16 00:54:07
【问题描述】:
当前数据库架构 -
我有这 3 张桌子,Curriculum、Assignment 和 Student。每个Student 通过学生和课程之间的ForeginKey 关系分配一个Curriculum。每一个
Assignment 与使用 ForeginKey 的 Curriculum 具有相同的关系。
问题陈述 -
每个Curriculum 大约有 100 个Assignments,问题是,一些学生需要免除一些作业,所以我想要一种方法,我可以免除学生的作业 1、2 和 3,但其余的都有的学生做作业 1、2 和 3。
我失败的解决方案 -
我尝试的是,在与Assignment 表相关的Student 表中创建一个ManyToManyField。然而,对于每个学生来说,必须手动添加数百个作业会非常耗时。
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)
【问题讨论】:
标签: django django-models database-design