Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
3356 Vistas

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 Mejor respuesta

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
Mejor respuesta

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

Publicaciones relacionadas Respuestas Vistas Actividad
1
jul 23
4299
2
sept 24
2236
1
ago 23
2031
1
ago 23
4143
1
sept 23
3227