跳至内容
菜单
此问题已终结
1 回复
3039 查看

Hello Odoo Community. 

I'm creating a field which calculate avg price sale order using this variables in Odoo v14:


for record in self:
        record['x_studio_orden_promedio'] = record.total_invoiced / record.sale_order_count


However when I try to see this field in action I get a divided zero error cause they are some clients that haven't make their first order.


How can I create an exception that print a result on this formula? Does custom fields can have exceptions?


Thanks in advance.

形象
丢弃

Use ZeroDivisionError Exception

try:
return self.number1/self.number2
except ZeroDivisionError:
return None

最佳答案

Hello Hassan Akil,

Please find below code it may help you,

for record in self:
record['x_studio_orden_promedio'] = record.total_invoiced / record.sale_order_count if record.sale_order_count else 0.0

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

形象
丢弃
编写者

Thank you so much for your help.

相关帖文 回复 查看 活动
3
5月 25
2012
1
4月 25
1506
3
9月 24
14535
2
2月 24
2649
0
12月 23
2064