Search code examples
pythondjangodjango-modelsaddition

I'm trying to perform math operation (specifically addition) with the contents of integer fields on my django models


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

Solution

  • It turns out that the input received from HTML comes as string. I had to convert each grade in the views.py file from request.POST['grade1'] to int(request.POST['grade1']) for each of the grades and used the self.grade1… method in models.py