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

17 thoughts on “Computed Fields

  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

Leave a Reply to Michael Cancel 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