Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
3007 Zobrazení

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
Zrušit

Use ZeroDivisionError Exception

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

Nejlepší odpověď

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
Zrušit
Autor

Thank you so much for your help.

Related Posts Odpovědi Zobrazení Aktivita
3
kvě 25
1742
1
dub 25
1290
3
zář 24
14204
2
úno 24
2474
0
pro 23
1960