Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
3008 Widoki

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.

Awatar
Odrzuć

Use ZeroDivisionError Exception

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

Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

Thank you so much for your help.

Powiązane posty Odpowiedzi Widoki Czynność
3
maj 25
1755
1
kwi 25
1295
3
wrz 24
14210
2
lut 24
2482
0
gru 23
1965