Development Book V17: Search Filter

In the search view, filters and grouping are utilized to refine records. Filtering is applied through the filter command based on specific conditions, and grouping is achieved using the group by command based on a designated field. To avoid the need for repetitive selection of "Add Custom Filter" or "Add Custom Group," a straightforward default setting is available. The filter tag within the search tag facilitates the addition of default filters.


                            from odoo import fields, models
                            
                            
                            class HospitalPatient(models.Model):
                               _name = "hospital.patient"
    
                               name = fields.Char(string="Name", help="Name of the applicant")
                               email = fields.Char(string="Email", help="Email of the applicant")
                               age = fields.Integer(string="Age", help="Age of the applicant")
                               phone = fields.Integer(string=Phone", help="Phone number of the applicant")
                               

                            <record id="hospital_patient_view_search" model="ir.ui.view">
                               <field name="name">hospital.patient.view.search</field>
                               <field name="model">hospital.patient</field>
                               <field name="arch" type="xml">
                                   <search>
                                       <field name="name"/>
                                       <field name="email"/>
                                       <field name="age"/>
                                       <field name="phone"/>
                                       <filter string="Archived" name="archived"
                                               domain="[('active', '=', False)]"/>
                                   </search>
                               </field>
                            </record>
                            

By selecting the "Archived" filter, you will access all records that have been archived.

odoo-development

The process of grouping is facilitated through the use of the "group by" command, which organizes records based on a specific field.


<record id="hospital_patient_view_search" model="ir.ui.view">
   <field name="name">hospital.patient.view.search</field>
   <field name="model">hospital.patient</field>
   <field name="arch" type="xml">
       <search>
           <field name="name"/>
           <field name="email"/>
           <field name="age"/>
           <field name="phone"/>
           <filter string="Archived" name="archived"
                   domain="[('active', '=', False)]"/>
           <group expand="0" string="Group By">
               <filter string="Age" name="age"
                       context="{'group_by':'age'}"/>
           </group>
       </search>
   </field>
</record>

In the code, we employ a "group by" operation based on the age field, passing data through the context. In this scenario, the records are grouped according to the specified age field. It's noteworthy that any field names from the relevant model can be utilized for grouping.

odoo-development
whatsapp
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