首页 > 解决方案 > 在创建数据库模式方面需要帮助

问题描述

当前数据库模式 - 我有这 3 个表,CurriculumAssignmentStudent. 每个都通过学生和课程之间的关系Student分配一个。每个都 与使用 ForeginKey具有相同的关系。CurriculumForeginKeyAssignmentCurriculum

问题陈述Assignments-每个大约有 100个Curriculum,问题是,一些学生需要免除一些作业,所以我想要一种方法可以让学生免除作业 1、2 和 3,但让其余学生完成作业 1、2 和 3。

我失败的解决方案- 我尝试的是,创建一个ManyToManyFieldStudent表格相关的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)

标签: djangodjango-modelsdatabase-design

解决方案


好的,所以我能想到的最佳解决方案是创建另一个表来存储所有豁免分配,称为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()]

上面的查询只会显示没有被豁免的作业。


推荐阅读