You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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:

1. Odoo 10 & 11: Is it possible to get 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

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.

2. Can we get 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.location model includes a built-in computed warehouse_id field. You can directly access it using location_id.warehouse_id without 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_id field 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:25:09