Odoo 10多仓库场景下点击仓库查看全库位产品方案问询
Got it, let's break down how to implement this feature in Odoo 10. The core idea is leveraging the warehouse's view_location_id (its top-level location) to fetch all child locations, then pulling in all products linked to those locations. Here's a step-by-step implementation:
Option 1: Add a Smart Button to Warehouse Form (Recommended)
This is the cleanest approach—add a button on the warehouse form that opens a filtered product list showing all items across the warehouse's locations.
1. Inherit the stock.warehouse Model
First, extend the warehouse model to add a product count field and an action method to open the product list.
from odoo import models, fields, api class StockWarehouse(models.Model): _inherit = 'stock.warehouse' product_count = fields.Integer(compute='_compute_product_count', string='Total Products') @api.multi def _compute_product_count(self): """Calculate the number of products in the warehouse's locations""" for warehouse in self: # Get all locations under the warehouse's top-level location location_ids = self.env['stock.location'].search([ ('id', 'child_of', warehouse.view_location_id.id) ]).ids # Count unique products from stock adjustment records (or use stock.quant for real-time stock) warehouse.product_count = len(self.env['stock.change.product.qty'].search([ ('location_id', 'in', location_ids) ]).mapped('product_id')) @api.multi def action_view_warehouse_products(self): """Open product list filtered to this warehouse's products""" self.ensure_one() location_ids = self.env['stock.location'].search([ ('id', 'child_of', self.view_location_id.id) ]).ids # Fetch unique products linked to the warehouse's locations product_ids = self.env['stock.change.product.qty'].search([ ('location_id', 'in', location_ids) ]).mapped('product_id').ids # Use the default product action and apply filters action = self.env.ref('product.product_product_action').read()[0] action['domain'] = [('id', 'in', product_ids)] action['context'] = {'default_warehouse_id': self.id} return action
2. Update the Warehouse Form View
Add the smart button to the warehouse form's button box using an XML inheritance.
<odoo> <record id="stock_warehouse_form_inherit_products" model="ir.ui.view"> <field name="name">stock.warehouse.form.products.inherit</field> <field name="model">stock.warehouse</field> <field name="inherit_id" ref="stock.view_warehouse_form"/> <field name="arch" type="xml"> <!-- Insert the button into the existing button box --> <xpath expr="//div[@name='button_box']" position="inside"> <button name="action_view_warehouse_products" type="object" string="View All Products" class="oe_stat_button" icon="fa-cubes"> <field name="product_count" widget="statinfo"/> </button> </xpath> </field> </record> </odoo>
Important Note: Use Real-Time Stock Instead of Adjustment Records
Wait—stock.change.product.qty tracks inventory adjustment history, not current stock levels. If you want to show products that are actually in stock right now, replace references to stock.change.product.qty with stock.quant (Odoo's real-time inventory model):
Updated Model Code for Real-Time Stock
@api.multi def _compute_product_count(self): for warehouse in self: location_ids = self.env['stock.location'].search([ ('id', 'child_of', warehouse.view_location_id.id) ]).ids # Count products with positive stock quantity warehouse.product_count = len(self.env['stock.quant'].search([ ('location_id', 'in', location_ids), ('quantity', '>', 0) ]).mapped('product_id')) @api.multi def action_view_warehouse_products(self): self.ensure_one() location_ids = self.env['stock.location'].search([ ('id', 'child_of', self.view_location_id.id) ]).ids product_ids = self.env['stock.quant'].search([ ('location_id', 'in', location_ids), ('quantity', '>', 0) ]).mapped('product_id').ids action = self.env.ref('product.product_product_action').read()[0] action['domain'] = [('id', 'in', product_ids)] action['context'] = {'search_default_in_stock': 1, 'default_warehouse_id': self.id} return action
How It Works
- The
child_ofdomain instock.locationsearch fetches every location under the warehouse's top-level view location (including the top-level itself). - We then collect all unique products linked to those locations, either from adjustment records or real-time stock.
- The smart button triggers an action that opens the standard product list, filtered to only show those products.
内容的提问来源于stack exchange,提问作者Ghada Rahhal

