Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
1739 Widoki
I added a field to the product listings to calculate the profit margin. I use this formula:

for record in self:

record['x_studio_margen_de_utilidad'] = (record.list_price - record.standard_price) / record.list_price

What condition should I apply to the field to avoid errors in the database?

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,


To prevent division-by-zero errors in your formula for calculating the profit margin in Odoo 17, you should ensure that record.list_price is not zero. This is important because dividing by zero will raise an exception and can disrupt the Odoo server or lead to incomplete record creation/updating.


You can wrap the calculation with a condition like this:


for record in self:
if record.list_price: # Implicitly checks that it's not zero or None
record['x_studio_margen_de_utilidad'] = (record.list_price - record.standard_price) / record.list_price
else:
record[
'x_studio_margen_de_utilidad'] = 0.0 # or any default value you prefe



If this is used in a computed field, make sure:


The field has store=True if you want it indexed and searchable.


You add both list_price and standard_price in the depends() decorator so that Odoo knows when to recompute.


Hope it helps

Awatar
Odrzuć
Najlepsza odpowiedź

I’d start by ensuring the universe doesn’t implode over a zero or negative list price—so, my first condition would be record.list_price > 0. After all, I'm not in the business of selling products with prices less than or equal to zero (unless we’ve entered some alternate dimension of reverse economics). Plus, dividing by zero isn’t just bad math; it’s a straight-up party pooper for your database. So, let’s keep things sensible and error-free!

And I guess sometimes you can sell product without any inventory which may result in standard price of 0 and the margin of 100%. 

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
sie 25
292
0
mar 25
1611
0
lut 25
1260
3
sie 24
5964
1
kwi 24
33