Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsupported operand type(s) for *: 'NoneType' and 'Decimal'

I have a django app that manages VAT. For instance, it can calculate the VAT for all sales from the European Union.

vat = purchases[0].vat.vat
eu_sales = Sale.objects.filter(country_type='2')
eu_amount = (eu_sales.aggregate(price = Sum('amount'))['price'])  * vat/100

However, today I copyied my files to another machine. Then I received an error

unsupported operand type(s) for *: 'NoneType' and 'Decimal'

I realised that I was getting this error because there is no eu_amount value stored yet. In overwords, there is no vale for eu_amount so it cannot multiply to a decimal value. Here is what I have in my template

{{eu_amount}}

Is there way to filter this template tag? I want to say something like if value the value is None, (or Null) then its 0.

like image 815
Shehzad009 Avatar asked Jan 18 '26 14:01

Shehzad009


1 Answers

You could try to use a default value like this.

eu_amount = (eu_sales.aggregate(price = Sum('amount'))['price']) or 0  * vat/100

But the cleanest way is probably to test if your Sum returns nothing, then to display some kind of warning message, because this should'nt happen.

like image 131
Thibault J Avatar answered Jan 21 '26 05:01

Thibault J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!