Computed Fields can be added in Odoo Studio or directly (by enabling “Developer” mode and navigating to Settings / Technical / Fields).
They can provide some useful functionality, as shown in the examples below. This does require some Python (mostly quite simple).
The first example is a ‘target’ (sales) price calculated from the cost price. This might be useful so sales staff can check that the sales price is reasonable.
Start by adding a field (type = float)

Select the field you added and then click on “More”

- Stored
- This should be set to FALSE in most cases.
- If it is set to TRUE, the value of the computed field will only be changed if the value of one of the dependent fields changes, but it does mean that you can use the field in filters, grouping, domain, etc.
- Dependencies
- Enter a list of field names:
- Used in the calculation (
standard_price
) - To trigger recalculation of the computed field (if it is set to “Stored”).
- Used in the calculation (
- Enter a list of field names:
- Compute (Python code)
for record in self:
record['x_studio_target_price'] = record.standard_price * 1.15
standard_price
is the cost price of the product.
Then save your changes.
Note: we can make this type of change for “custom fields” (whether created manually or by Odoo Studio), but if this is a “base field” you won’t be able to save your changes.
You can add further logic:
for record in self:
if record['standard_price'] !=0:
record['x_studio_target_price'] = record.standard_price * 1.15
More examples
Simple calculations
for record in self:
record['difference'] = close_meter_reading - open_meter_reading
for record in self:
record['total_price'] = ltr * unit_price
Cant find the ‘More’ button on latest Studio version.
LikeLike
Sorry for late reply. Normally that means you haven’t enabled Developer Mode. https://odootricks.tips/odoo-developer-debug-mode/
LikeLike
I want to divide price with tax (type = monetary) by quantity (type = float) how can I add a formula for that?
Do I need to convert float to monetary or is there any other option ???
Thanks in advance
LikeLike
This is a Python question and I am not a Python programmer!! You should be able to find Python tutorials online, or maybe some kind person will answer.
LikeLike
Hello,
I have a field called “x_studio_field_6rsgA” and I want to display in the name of my opportunity. How can I do that ?
This is my code:
for record in self:
record[‘x_studio_field_6rsgA’] = record.name
LikeLike
Yes
LikeLike
when subtracting dates, I get the following error “unsupported operand type(s) for -: ‘bool’ and ‘datetime.datetime'”
LikeLike
That sounds like a Python problem, and I have managed with the bare minimum of Python. Maybe someone else can offer some advice!
LikeLike
Can you post a larger code sample @Ben? The error is complaining that you’re trying to subtract a datetime from a boolean (e.g `False – datetime.datetime(…)`)
LikeLike
Thank you, the code is as follows:
Advanced Properties
Dependencies x_studio_initial_quote_date, x_studio_po_date – both are date fields
Compute
for record in self:
from_date = record.x_studio_initial_quote_date
to_date = record.x_studio_po_date
d1 = from_date
d2 = to_date
record[‘x_studio_days_to_close’] = (d1-d2).days
LikeLike
Interesting, for some reason it’s telling you that x_studio_initial_quote_date is a bool and not a datetime. Try adding a new text field called “x_my_debug_message” or something, and then do something like:
record[‘x_my_debug_message’] = ‘x_studio_initial_quote_date is type {}\nx_studio_po_date is type {}’.format(type(record.x_studio_initial_quote_date), type(record.x_studio_po_date))
and see what you get
LikeLike
for record in self:
record[‘x_studio_debug_message’] = type(record.x_studio_initial_quote_date)
‘type’ is not defined error and when use “format”, ‘format’ is not defined. but the following:
record[‘x_studio_debug_message’] = record.x_studio_initial_quote_date shows 2021-01-19 00:00:00
and
record[‘x_studio_debug_message’] = record.x_studio_po_date shows 2021-01-19
so, it seems I have a datetime field and a date field. I tried creating initial_quote_date_2 as date field (so both were date fields), but I received the same error
LikeLike
Ok, that’s very weird. The type() function is a builtin function in Python so it’s bizarre that it can’t be found. Also, str.format() was introduced in Python 2.6 which is ancient at this point so either you’re running pre-2.6 or something else is very wrong.
Anyway, try doing the following and see what you get:
record[‘x_studio_debug_message’] = record.x_studio_initial_quote_date – record.x_studio_po_date
LikeLike
After some digging and thinking, I realized that some of the “PO Dates” were empty because the quotes were not closed. As a result, the record.x_studio_po_date returned a boolean = False. The following works well, as long as both records are date fields. An error pops up when trying to find difference between fields date and datetime. thanks for the help!
for record in self:
if record[(‘x_studio_po_date’)] !=False and record[(‘x_studio_initial_quote_date_2’)] !=False:
d1 = record.x_studio_initial_quote_date_2
d2 = record.x_studio_po_date
record[‘x_studio_days_to_close’] = (d2-d1).days
else:
record[‘x_studio_days_to_close’] = 0
LikeLike
Glad it worked out!
LikeLike
Hi,
I wish to have a field “x_manufactured_weight” that will be available in the manufacturing order form view.
This field will be calculated by multiplying the weight of the product by the quantity produced.
How do i make the field “weight” from the product being manufactured available in the “Manufacturing Order” form view for computation?
LikeLike
You should be able to add a Related Field
LikeLike
Thanks Chris for pointing me in the right direction. Was able to setup a related field and use it to compute a desired value.
I need to format the result to 2 decimal places. I would really appreciate your help with this.
Again, thank you for the help.
LikeLike
I’m not a Python programmer, but it seems to be possible
LikeLike
You can also do precision via the XML/Qweb via something like t-options='{“widget”: “float”, “precision”: 2}’ inside of a span or div tag accordingly.
LikeLike
hi there was wondering i’m a newbie on Python and Odoo & i’m trying to figure out what i’m wrong, i’ve been trying to option
FIELD CREATE
field x_qty_left :float :read only
i did make both option work and visible for administrator, but i never been able to figure out how to make them visible also for public and portal user
i’ve tried a few ways but non of them worked
OPTION A: in shop product view, show the total amount sold
by displaying values of field product.sales_count
Tried :
record[‘x_sales_count’]= record.sales_count
I also tried
record[‘x_sales_count’]= record.sudo().sales_count
OPTION B: in shop product view show the total left to sold
with a simple calculation like product.x_total_limited_qty – product.sales_count = X_qty_left
i’ve tried
record[‘x_qty_left’] = product.x_total_limited_qty – product.sales_count
I also tried
record[‘x_qty_left’] = product.x_total_limited_qty – product.sudo().sales_count
resulting 403 page or calculation sales_count not substracted but ok for admin page access and calculation page for both options
i cna tsee what i’M doing wring is there something i forgot to add in the python formula?
LikeLike
Can we use this solution to compute commission bases salary which is calculated in Odoo spreadsheet to Odoo payroll?
LikeLike
I don’t understand the question. It’s already possible to calculate commission in the standard Odoo Payroll app, so what is that you want to do?
LikeLike
can i use computed into domain ,
i have courses each course must have one instructor , but instructor can’t assign to more than one course , so i want to get only instructors not selected before for any course ?
any idea !
LikeLike
Hello,
I’m trying to create a field in sales to show the amount of the 50% in advance payment to be able to use it on my document template then.
I created a monetary field with this calculation :
for record in self:
record[‘x_studio_monetary_field_g0irV’] = record.amount_total / 2
But it doesn’t work, the result is 0
The dependancies is amount_total
Where do I do it wrong ?
Thanks !
LikeLike