开发者

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

开发者 https://www.devze.com 2023-04-06 21:57 出处:网络
I have a django app that manages VAT. For instance, it can calculate the VAT for all sales from the European Union.

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

vat = purcha开发者_如何学Pythonses[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.


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.


Change the line vat = purchases[0].vat.vat to vat = purchases[0].vat.vat or 0


You mean this?

{% if item %}
    something
{% else %}
    something different
{% endif %}

Taken from https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs

I'm nearly sure, I've seen this .vat.vat somewhere lately ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消