She*_*009 3 python tags django 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
Run Code Online (Sandbox Code Playgroud)
然而,今天我将文件复制到另一台机器上。然后我收到一个错误
unsupported operand type(s) for *: 'NoneType' and 'Decimal'
我意识到我收到此错误是因为尚未eu_amount存储任何值。在 overwords 中,没有 val ,eu_amount因此它不能乘以十进制值。这是我的模板中的内容
{{eu_amount}}
Run Code Online (Sandbox Code Playgroud)
有没有办法过滤这个模板标签?我想说的是,如果 value 的值为 None(或 Null),则其值为 0。
您可以尝试使用这样的默认值。
eu_amount = (eu_sales.aggregate(price = Sum('amount'))['price']) or 0 * vat/100
Run Code Online (Sandbox Code Playgroud)
但最干净的方法可能是测试您的 Sum 是否不返回任何内容,然后显示某种警告消息,因为这种情况不应该发生。
| 归档时间: |
|
| 查看次数: |
8211 次 |
| 最近记录: |