Skip to Content
Menu
This question has been flagged
2 Replies
3291 Views

i am adding a field to compute length of field complete_name in product.category as below:

Name: x_complete_name_length

Type: Integer

Stored: True (Checked)

Readonly: True (Checked)

in ADVANCED PROPERTIES:

Dependencies: complete_name

Compute:

def get_length(self):

  if self.complete_name:

    self.x_complete_name_length=len(self.complete_name)

when trying to Save by pressing small button on top, it is throwing error:

raise ValueError("forbidden opcode(s) in %r: %s" % (expr, ', '.join(opname[x] for x in (code_codes - allowed_codes))))

ValueError: forbidden opcode(s) in 'lambda': STORE_ATTR


please help, error SEEMS not related but may be something inside somewhere which i don't know.

regards

Avatar
Discard
Author Best Answer

i have managed as below:

added custom field in product.category 

Name: x_parent_path_length 

Type: Integer 


created Automated Action as below:

Name: update parent path length

Model: Product Category (product.category)

Action to Do: Execute Python Code

Trigger: On Creation & Update


Python Code:

if record.parent_path:

  parent_length = len(record.parent_path)

  # raise Warning(parent_length)

  record.write({'x_parent_path_length': parent_length})

all done, now it is calculating length of field parent_path, have checked in pgAdmin

select * from product_category;

regards

Avatar
Discard
Best Answer

follow this way:
from odoo import fields, models


class ProductCategory(models.Model):

    _inherit = 'product.category'


    x_complete_name_length = fields.Integer(string="Complete Name Length", compute='_compute_complete_name_length', store=True, readonly=True)


    def _compute_complete_name_length(self):

        for category in self:

            if category.complete_name:

                category.x_complete_name_length = len(category.complete_name)

            else:

                category.x_complete_name_length = 0


The error you're encountering, "forbidden opcode(s) in 'lambda': STORE_ATTR," is due to the use of the self keyword within the lambda function, which is not allowed in Odoo field computation. Odoo's field computation function doesn't directly accept the self parameter.

Avatar
Discard
Author

thank you @Shubham, i already have resolved the problem adding Automated Action. and please note, i didn't used lambda function as all things done before AA is within View and code is already there in my Opening Post.

regards

Related Posts Replies Views Activity
1
Jul 23
4244
2
Sep 24
2184
1
Aug 23
1961
1
Aug 23
4086
1
Sep 23
3179