Home > Software design >  How to change the status of the column in the model in django
How to change the status of the column in the model in django

Time:11-11

I am creating an assignment management system, What a I want is whenever someone upload a submission in response to that assignment I want to change the status from pending, I will set everything on template page, but right now i feel like i have little issue with database.

class Assignment(models.Model):
    assignment_creator = models.ForeignKey(
        Teacher, on_delete=models.CASCADE, related_name="assignments")
    assignment_title = models.CharField(max_length=30)

class Submissions(models.Model):
    submitted_by = models.ForeignKey(Student, on_delete=models.CASCADE)
    submission_file = models.FileField(null=False, blank=True, default='')
    submitted_to = models.ForeignKey(
        Teacher, on_delete=models.CASCADE, null=True)
    submission_title = models.ForeignKey(
        Assignment, on_delete=models.CASCADE, null=True, blank=True)
    submission_status = models.BooleanField(default=False)

Is there any way to know which of the assignment related to that assignment title is uploaded so that I can change the status

CodePudding user response:

I don' t understand your design, because when a submission is created it should already be linked with the assignment (because a student makes a submission in response of an assignment). But if you really want to set submission_title later you could override the save method:

class Assignment(models.Model):
    assignment_creator = models.ForeignKey(
        Teacher, on_delete=models.CASCADE, related_name="assignments")
    assignment_title = models.CharField(max_length=30)

class Submissions(models.Model):
    submitted_by = models.ForeignKey(Student, on_delete=models.CASCADE)
    submission_file = models.FileField(null=False, blank=True, default='')
    submitted_to = models.ForeignKey(
        Teacher, on_delete=models.CASCADE, null=True)
    submission_title = models.ForeignKey(
        Assignment, on_delete=models.CASCADE, null=True, blank=True)
    submission_status = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        if self.submission_title != None and not submission_status:
           self.submission_status = True
        super.save(*args, **kwargs)
  • Related