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

Hello

I am trying to add a server action / contextual action to generate an internal reference for my manufactured products that do not have an internal reference already. The Python code that is executed looks like this:

for record in records:
  if record['route_ids' == 6]:
    if not record['default_code']:
      record['default_code'] = env['ir.sequence'].next_by_code('manufactured_product.default_code')
  else:
    raise UserError(_("Product is not manufactured."))

I've checked that the "Manufacture" route has ID 6. I want Odoo to only allow generation of internal reference for manufactured products. The problem is that if I try to run this server action on products that are not manufactured (only "Buy" route), the action still runs and generates an internal reference.

I'm obviously doing something wrong here. Anyone with a sharp eye who can see what's wrong with my code?

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

Hi,

Maybe the issue in your code is with the way you are checking if the product is manufactured or not. The condition if record['route_ids' == 6]: is not correct. You are trying to compare the 'route_ids' field with 6, but it's not the correct way to check if the product has a route with ID 6.

You should use the in operator to check if the route with ID 6 is in the 'route_ids' list. Here's the corrected code:

for record in records:
  if 6 in record['route_ids']:
    if not record['default_code']:
      record['default_code'] = env['ir.sequence'].next_by_code('manufactured_product.default_code')
  else:
    raise UserError(_("Product is not manufactured."))


Hope it helps

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you for the input! :-)
Unfortunately, it does not seem to work. Now, it will not generate the internal reference with any products. I'm not seeing any errors messages, but then I don't have any shell access so I wouldn't know. But it doesn't look like the conditions are met.

Tác giả Câu trả lời hay nhất

So using ChatGPT I was able to find a way that worked. I think there were some problems accessing the related field in stock.route from the server actions context. Here is the code that actually works:

for record in records:
    try:
        # Use sudo() to execute the operation with elevated privileges
        route_ids = record.sudo().route_ids.ids
       
        if 6 in route_ids:
            if not record.default_code:
                # Use Odoo API to update the record
                record.sudo().write({
                    'default_code': env['ir.sequence'].next_by_code('manufactured_product.default_code')
                })
    except Exception as e:
        # Handle the exception, print or log it as needed
        print(f"Error processing record: {e}")



I've got some concerns as to the security of this code. Any thoughts?


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 6 23
11064
0
thg 3 15
3681
2
thg 3 15
7082
1
thg 3 24
1930
4
thg 10 23
12664