Skip to Content
मेन्यू
This question has been flagged
1 Reply
3015 Views

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.

Avatar
Discard

Use ZeroDivisionError Exception

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

Best Answer

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

Avatar
Discard
Author

Thank you so much for your help.

Related Posts Replies Views Activity
3
मई 25
1833
1
अप्रैल 25
1370
3
सित॰ 24
14318
2
फ़र॰ 24
2540
0
दिस॰ 23
1970