1
votes

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.

1

1 Answers

1
votes

It's not related to the One2many field, it's related to the move_lines object (which is stock.move model), and takes from that model product_id field, which has a type of Many2one. So, everything is correct. Here's the code.