【问题标题】:Need help in creating a database schema在创建数据库模式方面需要帮助
【发布时间】:2020-02-16 00:54:07
【问题描述】:

当前数据库架构 - 我有这 3 张桌子,CurriculumAssignmentStudent。每个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


    【解决方案1】:

    好的,所以我能想到的最佳解决方案是创建另一个表来存储所有豁免分配,称为 ExemptAssignements

    models.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()]
    

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

    【讨论】:

      猜你喜欢
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2022-07-04
      • 2010-10-06
      • 2011-07-17
      相关资源
      最近更新 更多