Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
3009 Tampilan

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
Buang

Use ZeroDivisionError Exception

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

Jawaban Terbai

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
Buang
Penulis

Thank you so much for your help.

Post Terkait Replies Tampilan Aktivitas
3
Mei 25
1760
1
Apr 25
1295
3
Sep 24
14213
2
Feb 24
2483
0
Des 23
1965