Add Filter and Group By to Search View in Odoo

In Odoo, you can add a filter and group to the search view of a model. This is useful if you want to restrict the data that is displayed in the search view, or if you want to group the data by a certain field.

To add a filter and group by to the search view, you first need to create a search view.

Modify Existing Search View

Similar to inherit a view id, you can find the id of a search view then use it as inherit_id.

The snippet below adds a new Filter called Has External Reference and group by product to search view in product template list page.

<record model="ir.ui.view" id="custom_view_product_search_form_inherit">
    <field name="name">product.template.search.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_search_view"/>
    <field name="arch" type="xml">
        <xpath expr="//search/filter[@name='consumable']" position="after">
            <filter string="Has External Reference" name="filter_lazada" domain="[('has_external_reference','!=', '')]"/>
            <group expand="0" name="group_by" string="Group By">
                <filter name="group_product" string="Product" domain="[]"
                        context="{'group_by' : 'product_id'}"/>
            </group>
        </xpath>
    </field>
</record>

New Search View

This approach is used for custom model you added to the Odoo system. You need to determine a search view in view_id field of a custom model view’s definition.

<record model="ir.actions.act_window" id="action_my_model">
    <field name="name">My Model</field>
    <field name="res_model">my.model</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="view_id" ref="my_model_view_tree"/>
    <field name="search_view_id" ref="my_model_search_form_view"/>
</record>

Then create a view with the respective id.

<record id="my_model_search_form_view" model="ir.ui.view">
    <field name="name">my.model.search.form</field>
    <field name="model">my.model</field>
    <field name="mode">primary</field>
    <field name="arch" type="xml">
        <search string="My Model">
            <field name="name" string="Name"
                   filter_domain="['|', '|', ('default_code', 'ilike', self), ('name', 'ilike', self), ('barcode', 'ilike', self)]"/>
            <filter string="State" name="confirm" domain="[('state','=','confirm')]" help="Confirmed Data"/>
            <group expand="0" name="group_by" string="Group By">
                <filter name="group_product" string="Product" domain="[]"
                        context="{'group_by' : 'product_id'}"/>
            </group>
        </search>
    </field>
</record>

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close