I want to add max value to my model field based on the data of another model which is related to it through one to many relationship

Image of django admin panel

This are my two models:

class English(models.Model):
    date = models.DateField()
    topic = models.CharField(max_length=150)
    totalMarks = models.IntegerField()

    def __str__(self):
        return f"{self.id}. {self.topic}"

    class Meta:
        verbose_name_plural = "English"


class EnglishMarks(models.Model):
    student = models.ForeignKey(UserField, on_delete=CASCADE)
    test = models.ForeignKey(English, on_delete=CASCADE, related_name="marks")
    marks = models.IntegerField(validators=[MaxValueValidator(English.objects.first().totalMarks),MinValueValidator(0)])

I want the marks field of EnglishMarks Model to not exceed the value of totalMarks field of related data of English Model.

Note: I am using Inlines to populate EnglishMarks table while creating new data in English table

Back to Top