Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
3388 Widoki

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

Awatar
Odrzuć
Autor Najlepsza odpowiedź

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

Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
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

Powiązane posty Odpowiedzi Widoki Czynność
1
lip 23
4336
2
wrz 24
2251
1
sie 23
2070
1
sie 23
4162
1
wrz 23
3251