在Odoo 10、11及全版本中,能否通过location_id获取warehouse_id?
Hey there! Let's tackle your two questions about retrieving warehouse_id from location_id in Odoo step by step:
warehouse_id from location_id? Absolutely! While the stock.location model in these versions doesn't come with a direct warehouse_id field out of the box, the underlying relationship between locations and warehouses still exists. Warehouses are linked to specific location hierarchies (like the view location, stock location, input/output locations), so you can compute the associated warehouse with a bit of custom code.
Here are two reliable approaches:
Approach 1: Reverse search warehouse via location hierarchy
You can add a computed field to stock.location that searches for warehouses where the current location falls under the warehouse's view location hierarchy:
from odoo import fields, models class StockLocation(models.Model): _inherit = 'stock.location' warehouse_id = fields.Many2one('stock.warehouse', string='Associated Warehouse', compute='_compute_warehouse_id') def _compute_warehouse_id(self): for location in self: # Find the warehouse whose view location hierarchy includes this location warehouse = self.env['stock.warehouse'].search( [('view_location_id', 'parent_of', location.id)], limit=1 ) location.warehouse_id = warehouse
Approach 2: Traverse parent locations to find warehouse links
If you want to check if the location (or any of its parents) is directly linked to a warehouse's core locations (stock, input, output, view), use this method:
def _compute_warehouse_id(self): for location in self: current_loc = location linked_warehouse = False while current_loc and not linked_warehouse: # Check if current location is one of the warehouse's key locations linked_warehouse = self.env['stock.warehouse'].search([ '|', '|', '|', ('lot_stock_id', '=', current_loc.id), ('view_location_id', '=', current_loc.id), ('input_location_id', '=', current_loc.id), ('output_location_id', '=', current_loc.id) ], limit=1) current_loc = current_loc.parent_id location.warehouse_id = linked_warehouse
Both methods work perfectly in Odoo 10 and 11—just add this code to a custom module and install it.
warehouse_id from location_id in all Odoo versions? Yes, you can achieve this across all Odoo versions, though the implementation varies slightly:
- Odoo 12 and later: The
stock.locationmodel includes a built-in computedwarehouse_idfield. You can directly access it usinglocation_id.warehouse_idwithout writing any custom code. This was added to simplify warehouse-location associations out of the box. - Odoo 9 and earlier: Similar to Odoo 10/11, there's no native
warehouse_idfield on locations. You'll need to implement a custom computed field using logic similar to the approaches above. The warehouse-location relationship structure is consistent in these older versions, so the same search/traversal logic will work.
In short, no matter which Odoo version you're working with, you can always retrieve the associated warehouse_id from a location_id—either using the native field (12+) or a simple custom computation (earlier versions).
内容的提问来源于stack exchange,提问作者Alpesh Valaki

