Skip to Content
Menú
This question has been flagged
2 Respostes
3324 Vistes

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
Descartar
Autor 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
Descartar
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
Descartar
Autor

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 Respostes Vistes Activitat
1
de jul. 23
4270
2
de set. 24
2212
1
d’ag. 23
1996
1
d’ag. 23
4117
1
de set. 23
3204