I seem to be having a problem multiplying in Django. I want to multiply the Total Purchase including VAT with my VAT value. Unfortunately, I got this error.
unsupported operand type(s) for *: 'Decimal' and 'VAT'
It looks like my program calls vat (see views) `’VAT’ rather than decimal. I’m not sure why there is an error unless there is another way around it.
Views.py
purchases = Purchase.objects.all()
vat = purchases[0].vat
total_purchases = Purchase.objects.all().aggregate(price = Sum('amount'))
total_purchases_vat = total_purchases['price'] * vat
models.py
class VAT(models.Model):
vat = models.DecimalField(max_digits = 4, decimal_places = 1)
def __unicode__(self):
return unicode(self.vat)
class Purchase(models.Model):
vat_period = models.ForeignKey(VAT_Period)
date = models.DateField()
amount = models.DecimalField(max_digits=20, decimal_places=2)
description = models.TextField(max_length = 400)
vat = models.ForeignKey(VAT)
def __unicode__(self):
return unicode(self.amount)
purchases[0].vatyields an instance of theVATmodel. You need to usepurchases[0].vat.vatto get the actual value.