I had written a function inside models.py which will calculate percentage. But it not showing the calculated values.
I have written a function called 'cal_amount' in models.py performed the calculation. return the value to the model field. But it showing None when i called.
class Course(models.Model):
price = models.FloatField("Price", blank=True, null=True)
voucher_id = models.CharField(max_length=255,blank=True, null=True)
voucher_amount = models.IntegerField(blank=True, null=True)
discounted_amount = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.course_name
def cal_amount(self):
self.discounted_amount = (self.voucher_amount/100)*self.price
return self.discounted_amount
What i want is calculated amount to be stored in discounted_amount. so i can use {{ obj.discounted_amount }} in html to view it along with actual price. Please suggest me a way.
data = Course.objects.get(course_name=self.course_name)after thisdata. cal_amount()and thendata.save()