Jason
Jason

Reputation: 21

How to create computed field in Odoo Studio using Python code?

I am using Studio in Odoo version 10.0. I successfully created the field with the name x_studio_field_dZVpy that appears in the product.template GUI.

When I try to edit the product name in the product.template GUI it gives me a Value Error: forbidden opcode(s) in 'lambda'.

I checked the "readonly" and "stored" check boxes. In the "dependencies" field I entered "name". I entered the following into the "compute" field in "Advanced Properties" section of the field.

I entered the following into the "compute" field in "Advanced Properties" section of the field.

def compute_product_dimension(self):
    for record in self:
        if product.name[:2] == 'LG':
            product_specs = product.name.split('-') 
            product_dimension = float(product_specs[6])
            x_studio_field_dZVpy = product_dimension / 2
        else:
            x_studio_field_dZVpy = ""

For example

product.name= LG-611-40M-3UM-95P-8.000

If the first 2 characters of the product.name is "LG" the code splits the string into an array and divides the 6th element in the array by 2. In this example this should divide 8.000 by 2. The "x_studio_field_dZVpy" field should then display 4.000.

screenshot of Odoo Studio GUI with code

Upvotes: 2

Views: 3132

Answers (2)

Jason
Jason

Reputation: 21

Computed fields in Odoo must have stored = false (unchecked). Dependent fields (name in this case) must have stored = true (checked).

Another error was Python arrays are zero based for example: product_specs[1] returns 611. Revised product_specs[6] to product_specs[5]. I renamed the computed field x_product_dimension. This solved the problem.

revised code

for record in self:
    if product.name[:2] == 'LG':
        product_specs = product.name.split('-') 
        product_dimension = float(product_specs[5])
        record['x_product_dimension'] = product_dimension / 2
    else:
        record['x_product_dimension'] = ""

Upvotes: 0

Lucas
Lucas

Reputation: 894

Instead of:

for record in self:
    if name[:2] == 'LG':

Try:

for product in self:
    if product.name[:2] == 'LG':

Upvotes: 1

Related Questions