If you are running a warehouse where your products come with attributes, such as T-shirts in various sizes and colors or electronic devices with varying storage capacities, you will understand what we are talking about. The default view of stock inventory of Odoo allows you to filter products by their names, lots, and locations, but there is no way to filter according to attributes.
In this article, we will explain how to create a simple module from scratch, which would make it possible for us to filter our stock inventory according to attribute values.
What We're Building
The Odoo minimal module named product_attr_search which extends the stock.quant by adding a search feature. After installation of this module, entering an attribute such as “Red” or “128GB” in the search box will display only those quants having the attribute value entered.
Module File Structure
product_attr_search/
+-- __init__.py
+-- __manifest__.py
+-- models/
¦ +-- __init__.py
¦ +-- stock_quant.py
+-- views/
+-- stock_quant_views.xml
The Manifest
The manifest tells Odoo what this module is and what it depends on. We only need the stock module as a dependency since that's where stock.quant and its search view live.
{
'name': 'Product Attribute Search',
'version': '19.0.1.0.0',
'depends': ['stock'],
'data': [
'views/stock_quant_views.xml',
],
}The Model Extension
This is the most important file. We inherit stock.quant and add a computed Many2many field that represents the attribute values of each quant's product variant, along with a _search method that Odoo will call when this field is used as a search criterion.
from odoo import models, fields, api
class StockQuant(models.Model):
_inherit = 'stock.quant'
product_attribute_value_ids = fields.Many2many(
'product.attribute.value',
string='Attribute Values',
compute='_compute_product_attribute_value_ids',
search='_search_product_attribute_value_ids',
store=False,
)
@api.depends('product_id.product_template_attribute_value_ids')
def _compute_product_attribute_value_ids(self):
for quant in self:
quant.product_attribute_value_ids = \
quant.product_id.product_template_attribute_value_ids.mapped(
'product_attribute_value_id'
)
def _search_product_attribute_value_ids(self, operator, value):
# Find matching attribute values by name
attr_values = self.env['product.attribute.value'].search([
('name', 'ilike', value)
])
if not attr_values:
return [('id', '=', False)]
# Find product variants that carry any of those attribute values
products = self.env['product.product'].search([
('product_template_attribute_value_ids.product_attribute_value_id',
'in', attr_values.ids)
])
if not products:
return [('id', '=', False)]
return [('product_id', 'in', products.ids)]
The search method
def _search_product_attribute_value_ids(self, operator, value):
attr_values = self.env['product.attribute.value'].search([
('name', 'ilike', value)
])
if not attr_values:
return [('id', '=', False)]
products = self.env['product.product'].search([
('product_template_attribute_value_ids.product_attribute_value_id',
'in', attr_values.ids)
])
if not products:
return [('id', '=', False)]
return [('product_id', 'in', products.ids)]
When a user types something in the search bar, Odoo calls this method with operator='ilike' and value=<what they typed>. The method must return a standard Odoo domain (a list of tuples) that will be applied to stock.quant.
The Search View
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="stock_quant_search_view_inherit" model="ir.ui.view">
<field name="name">stock.quant.search.inherit</field>
<field name="model">stock.quant</field>
<field name="inherit_id" ref="stock.quant_search_view"/>
<field name="arch" type="xml">
<xpath expr="//search" position="inside">
<field name="product_attribute_value_ids"
string="Attribute Value"
filter_domain="[('product_id.product_template_attribute_value_ids.product_attribute_value_id.name', 'ilike', self)]"/>
</xpath>
</field>
</record>
</odoo>
Odoo 19's product variant system is powerful, but its out-of-the-box search capabilities don't always keep up with how warehouse teams actually think about stock. People working in a warehouse don't search by product name — they search by what makes a variant distinct: the color, the size, the configuration.
To read more about An Overview of Product Variant Management in Odoo 19, refer to our blog An Overview of Product Variant Management in Odoo 19.