Skip to Content
Menu
This question has been flagged
5630 Views

I'm confused by something in the Odoo source code. On the `stock.picking` model, there is a `product_id` field. It's defined as a related field via `move_lines.product_id`.


`move_lines` is a one2many field. I don't understand how a many2one field can use a one2many field as its relation.


Here's a link to the source code I'm referring to:


https://github.com/odoo/odoo/blob/316ffc80147de076b28c6156ac679dd90da0935e/addons/stock/models/stock_picking.py#L288


You can see that `product_id` is defined as:


    product_id = fields.Many2one('product.product', 'Product', related='move_lines.product_id')


And `move_lines` is defined as:


    move_lines = fields.One2many('stock.move', 'picking_id', string="Stock Moves", copy=True)


What is the purpose of this definition? How is it even allowed?


If I look at the value of the `product_id` field for a picking, it returns the product for the first move line in the picking, not all of products.


However, if I search the picking tree view with a custom filter on the Product field, for example, `Product contains 'Product Name'`, the results seem to account for all products in the picking. If I search for any product in the picking the picking appears in the view, it's not just limited to the first product.


Can someone explain this behavior? There is even a note in the source code that the `product_id` field is specifically for searching, so I'm thinking there is some magic functionality I never knew about.

Avatar
Discard
Related Posts Replies Views Activity
3
Sep 24
9979
2
Feb 24
597
1
Jul 23
1003
2
Jun 23
1394
1
May 23
11450