python - * : 'NoneType' and 'Decimal' 不支持的操作数类型

标签 python django tags null

我有一个管理增值税的 django 应用程序。例如,它可以计算来自欧盟的所有销售的增值税。

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

但是,今天我将文件复制到另一台机器上。然后我收到一个错误

* 不支持的操作数类型:“NoneType”和“Decimal”

我意识到我收到此错误是因为尚未存储 eu_amount 值。在 overwords 中,没有 eu_amount 值,因此它不能乘以十进制值。这是我的模板中的内容

{{eu_amount}}

有办法过滤这个模板标签吗?我想说的是,如果 value 的值为 None(或 Null),则其值为 0。

最佳答案

您可以尝试使用这样的默认值。

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

但最干净的方法可能是测试您的 Sum 是否不返回任何内容,然后显示某种警告消息,因为这不应该发生。

关于python - * : 'NoneType' and 'Decimal' 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514943/

相关文章:

python - 对直方图定义的分布进行反卷积

python - Django_Hosts 导入错误

django - 我如何检测 Django 中终止的连接?

iphone - iPhone SDK 的 Mac OS 中是否存在标签字段或类似标签的内容?

php - 获取所有文本但不是正则表达式匹配

html - 我的 main.css 没有链接到我电脑的 html 文件 "outside"

python - super 对象的属性

Python PDF 库

python - 如何用python对列值求和

python - 如何将 django FileField 保存到用户文件夹?