Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
146 Lượt xem

Basically I was adding 2 fields to the form view: Material to replace and Replacement material, then I would give the option to select multiple BOMs and each of them will replace the material selected with the replacement material. My problem was when I tried adding the code, It gives me forbidden opcode(s) error.

Im using odoo studio btw.

This is the code:

from odoo.exceptions import UserError


# Get the material to replace and the replacement material from the FIRST selected record

if records:

    first_record = records[0]

    material_to_replace = first_record.x_studio_material_to_replace_1

    replacement_material = first_record.x_studio_replacement_material_1


    if not material_to_replace or not replacement_material:

        raise UserError("Please specify both 'Material to Replace' and 'Replacement Material' in the first selected BOM.")


    # Loop through the selected BOMs

    for bom in records:

        # Loop through the BOM lines

        for line in bom.bom_line_ids:

            # Check if the material matches the one to replace

            if line.product_id == material_to_replace:

                # Replace the material

                line.product_id = replacement_material

                line.product_qty = line.product_qty #This line is added to force the recomputation of values

else:

    raise UserError("No BOMs selected.")


And this is the error:  forbidden opcode(s) in 'from odoo.exceptions import UserError\n\n# Get the material to replace and the replacement material from the FIRST selected record\nif records:\n    first_record = records[0]\n    material_to_replace = first_record.x_material_to_replace\n    replacement_material = first_record.x_replacement_material\n\n    if not material_to_replace or not replacement_material:\n        raise UserError("Please specify both \'Material to Replace\' and \'Replacement Material\' in the first selected BOM.")\n\n    # Loop through the selected BOMs\n    for bom in records:\n        # Loop through the BOM lines\n        for line in bom.bom_line_ids:\n            # Check if the material matches the one to replace\n            if line.product_id == material_to_replace:\n                # Replace the material\n                line.product_id = replacement_material\n                line.product_qty = line.product_qty #This line is added to force the recomputation of values\nelse:\n    raise UserError("No BOMs selected.")': IMPORT_NAME, IMPORT_FROM, STORE_ATTR

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Please refer to the code:


if not records:

    raise UserError("No BOMs selected.")


first_record = records[0]

material_to_replace = first_record.x_studio_material_to_replace_1

replacement_material = first_record.x_studio_replacement_material_1


if not material_to_replace or not replacement_material:

    raise UserError(

        "Please specify both 'Material to Replace' and 'Replacement Material' in the first selected BOM.")


# Loop through the selected BOMs

for bom in records:

    for line in bom.bom_line_ids:

        if line.product_id == material_to_replace:

            # Use write() instead of direct assignment

            line.write({

                "product_id": replacement_material.id,

            })


Hope it helps.

Ảnh đại diện
Huỷ bỏ

You should use assignment and let the ORM manage the database updates.

Câu trả lời hay nhất

You cannot import any libraries, UserError is already available.

All record assignments must be in the form:

record['field'] = value
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 8 25
1116
3
thg 7 25
632
2
thg 7 25
1202
1
thg 5 25
932
0
thg 5 25
1087