Default filters and group by in Odoo

We can set default grouping and filters in Actions from XML views definitions.

I’m taking example from a custom search view upon stock.move model:

<record model="ir.ui.view" id="custom_view_stock_search">
    <field name="name">stock.move.custom.search</field>
    <field name="model">stock.move</field>
    <field name="arch" type="xml">
    <search string="Stock Moves">
       <field name="origin" filter_domain="['|', '|', ('origin', 'ilike', self), ('name', 'ilike', self), ('picking_id', 'ilike', self)]" string="Reference"/>
       <field name="date" groups="base.group_no_one"/>
           <separator/>
       <filter string="Incoming" name="incoming" domain="[('location_id.usage', 'not in', ('internal', 'transit')), ('location_dest_id.usage', 'in', ('internal', 'transit'))]"/>
       <filter string="Outgoing" name="outgoing" domain="[('location_id.usage', 'in', ('internal', 'transit')), ('location_dest_id.usage', 'not in', ('internal', 'transit'))]"/>
       <group expand="0" string="Group By">
           <filter string="Owner" name="owner" domain="[]" context="{'group_by':'owner_id'}"/>
           <filter string="Product" name="by_product" domain="[]" context="{'group_by':'product_id'}"/>
       </group>
    </search>
    </field>
</record>

In the action we have also the example using two fields of group by:

<record id="stock_move_treeview_custom_action" model="ir.actions.act_window">
     <field name="name">Conto vendita</field>
     <field name="type">ir.actions.act_window</field>
     <field name="res_model">stock.move</field>
     <field name="view_mode">tree,form</field>
     <field name="view_type">form</field>
     <field name="view_id" ref="stock_move_treeview_custom"/>
     <field name="search_view_id" ref="custom_view_stock_search"/>
     <field name="context">{'group_by':['owner_id', 'product_id'],'search_default_outgoing': 1}</field>
</record>

In the action context we have used a group by two fields, we can specify only one or refer to a filter name of the search view in the following ways:

<field name="context">{'search_default_group_owner': 1}</field>

<field name="context">{'group_by': 'owner_id'}</field>