Computed Fields

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”

  1. 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.
  2. Dependencies
    • Enter a list of field names:
      1. Used in the calculation (standard_price)
      2. To trigger recalculation of the computed field (if it is set to “Stored”).
  3. 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 

More complex usage

  1. Computed Field with count of POS orders
  2. Use Automated Actions to stop users archiving products
  3. Use a computed field to set field attributes
  4. Add tracking using Odoo Studio
  5. Sales Order Delivery Status

23 thoughts on “Computed Fields

      1. 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

        Like

      2. 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.

        Like

  1. 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

    Like

  2. when subtracting dates, I get the following error “unsupported operand type(s) for -: ‘bool’ and ‘datetime.datetime'”

    Like

    1. That sounds like a Python problem, and I have managed with the bare minimum of Python. Maybe someone else can offer some advice!

      Like

  3. 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(…)`)

    Like

    1. 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

      Like

      1. 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

        Like

      2. 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

        Like

      3. 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

        Like

      4. 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

        Like

  4. 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?

    Like

      1. 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.

        Like

  5. 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.

    Like

  6. 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?

    Like

  7. Can we use this solution to compute commission bases salary which is calculated in Odoo spreadsheet to Odoo payroll?

    Like

    1. 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?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s