Skip to Content
Menu
This question has been flagged

For the purposes of this question say I have the following tables Orders, Order Items, Items, Item Components and Components.
Orders with

  • Items (one2many field connecting to Order-Items)

Order-Items has

  • Order (many2one)
  • Item (many2one)

Items

  • Components (one2many field connecting to Item-Components)

Item Components has

  • Item (many2one)
  • Component(many2one)


I need a way to generate a list of all the component involved in an order, aka I need to pull the Item-Component details for every Item on the Order. I don't even particularly care whether or not it is broken out by part, but I can only use xml, computed fields, or server actions because I'm using Odoo Studio w/ Odoo Online.


I have tried to find a way to do it through related fields.
I have tried to find a way to do it through on views.
I have tried to find a way to do it through report views, although that is less than ideal.

At this point unless someone knows a trick for one of the above methods, I'm looking for a way to setup a server action to create a new record for every line in a one to many table so I can setup a Order-Item-Component table with the item-component details added every time an item is added to an order, but i'm not sure how to accomplish that inside of a server action. There is a ton of documentation about how to write into a one2many field, but I wasn't finding much about iterating out of a one2many field.


Hopefully, this isn't like the other night when I was trying to test an error message and getting frustrated it was throwing an error message. (Lesson there, despite the majority of the documentation still saying to use raise Warning, if you want it to be user-friendly you need to use raise UserError. But that doesn't mean that Warning won't raise an error :D)

Avatar
Discard
Author Best Answer

I figured out the Order-Item-Component table code. I still feel like I'm doing more work than neccessary, but at least its a work around.

Server Action w/ Model as Order-Items and Action as Execute Python Code

Model

Field

Field Type

order
idinteger
orderx_order_itemsone2many (order_items via x_order_id)
itemidinteger
item
x_item_components
one2many (item_components via x_item_id)
order_items
idinteger
order_items
x_order_id
many2one (order)
order_itemsx_item_idmany2one (item)
item_componentsidinteger
item_components
x_item_idmany2one (item)
item_components
x_component_idmany2one (component)
order_item_compomentsx_item_idmany2one (item)
order_item_compoments
x_item_component_idmany2one (item_components)
order_item_compoments
x_order_idmany2one (order)
order_item_compoments
x_order_item_idmany2one (order_items)


order_var = record.x_order_id.id
item_var = record.x_item_id.id
order_item_var = record.id
addList = record.x_item_id.x_item_components.mapped('id') #pulls a list ids for all lines of the one2many x_item_components field

for component in addList:
  vals=dict(x_item_id = item_var, x_item_component_id = component, x_order_id = order_var, x_order_item_id = order_item_var)
  env['order_item_compoments'].create(vals)


Avatar
Discard
Related Posts Replies Views Activity
1
Sep 23
1869
1
Jan 24
1889
3
Nov 23
3283
1
Dec 22
2736
2
Nov 23
2775