Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
3342 Tampilan

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
Buang
Penulis Jawaban Terbai

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
Buang
Jawaban Terbai

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
Buang
Penulis

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

Post Terkait Replies Tampilan Aktivitas
1
Jul 23
4288
2
Sep 24
2230
1
Agu 23
2023
1
Agu 23
4131
1
Sep 23
3220