Enable Dark Mode!
how-to-search-product-variants-by-attribute-value-in-odoo-19.jpg
By: Milton Tom

How to Search Product Variants by Attribute Value in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

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.


Frequently Asked Questions

Do I need to add security rules or access rights for this module?

No. The module doesn't define any new models or new records — it only extends an existing model and an existing view. Users who already have access to the stock inventory view will automatically have access to the new search field. No ir.rule or ir.model.access records are needed.

Can I add this search to other views, like delivery orders or sales order lines?

Yes. The model chain is slightly different for each view, but the pattern is identical. For stock.move.line (detailed operations in a delivery), the path would be product_id.product_template_attribute_value_ids.product_attribute_value_id.name — exactly the same dotted path, just applied to a different parent model. Inherit the relevant search view and add the field with the appropriate filter_domain.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message