Django form Data

Viewed 21

Please I gotta a model I created with three fields input1,input2,total. So I generated a model form so that if I input the values of input 1 and input2 it will automatically multiply the the inputted values. Then on calling the save method it will save the inputted values and computed value to the database

1 Answers

This should work for you..

class Multiply(models.Model):
input1 = models.IntegerField()
input2 = models.IntegerField()
result = models.IntegerField(null=True, blank=True)

def __str__(self):
    return str(self.input1) + " * " + str(self.input2) + " = " + str(self.result)

def save(self, *args, **kwargs):
    self.result = self.input1 * self.input2
    super().save(*args, **kwargs)
Related