Home > Back-end >  I'm trying to perform math operation (specifically addition) with the contents of integer field
I'm trying to perform math operation (specifically addition) with the contents of integer field

Time:01-13

I'm trying to perform math operation (specifically addition) with the values of integer fields on my django models but i kept getting this warning even before running the program: "Class 'IntegerField' does not define 'add', so the ' ' operator cannot be used on its instances"

this is my django model code:

class Applicants(models.Model):
    fname = models.CharField(max_length=255)
    lname = models.CharField(max_length=255)
    number = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    gender = models.CharField(max_length=255)
    p_course = models.CharField(max_length=255)
    o_course = models.CharField(max_length=255)
    grade1 = models.IntegerField(max_length=255)
    grade2 = models.IntegerField(max_length=255)
    grade3 = models.IntegerField(max_length=255)
    grade4 = models.IntegerField(max_length=255)
    grade5 = models.IntegerField(max_length=255)
    grade6 = models.IntegerField(max_length=255)
    total_grade = grade1   grade2   grade3   grade4   grade4   grade5

CodePudding user response:

class Applicants(models.Model):
    fname = models.CharField(max_length=255)
    lname = models.CharField(max_length=255)
    number = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    gender = models.CharField(max_length=255)
    p_course = models.CharField(max_length=255)
    o_course = models.CharField(max_length=255)
    grade1 = models.IntegerField()
    grade2 = models.IntegerField()
    grade3 = models.IntegerField()
    grade4 = models.IntegerField()
    grade5 = models.IntegerField()
    grade6 = models.IntegerField()
    
    @property
    def get_sum_of_grades(self):
    total_grade = self.grade1   self.grade2   self.grade3   self.grade4   self.grade4   self.grade5
        return total_grade

CodePudding user response:

Model.clean()¶

This method should be used to provide custom model validation, and to modify attributes on your model if desired. For instance, you could use it to automatically provide a value for a field, or to do validation that requires access to more than a single field (source)

As described here, you should implement a clean method to your model:

class Applicants(models.Model):
    fname = models.CharField(max_length=255)
    lname = models.CharField(max_length=255)
    number = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    gender = models.CharField(max_length=255)
    p_course = models.CharField(max_length=255)
    o_course = models.CharField(max_length=255)
    grade1 = models.IntegerField()
    grade2 = models.IntegerField()
    grade3 = models.IntegerField()  # btw Integerfields don't have `max_lenght`
    grade4 = models.IntegerField()
    grade5 = models.IntegerField()
    grade6 = models.IntegerField()
    total_grade = models.IntegerField()

    def clean(self):
        super().clean()
        self.total_grade = sum(self.grade1, self.grade2, self.grade3, self.grade4, self.grade4, self.grade5)

For sure that total_grade field will be shown in a form if you choose to use a models.ModelForm but you can go for exclude = ['total_grade'] to hide it. Be careful, the result of the computation does not reach "normal" form validation, as it runs before within the statement super().clean()

  • Related