How To Define Search View In Odoo 16

How To Define Search View In Odoo 16

 

How To Define Search View In Odoo 16

In Odoo 16, you can define a search view in a module’s XML file using the search tag. Here’s an example of how to define a search view for a model named patient.py:

<record id=”view_hospital_patient_search” model=”ir.ui.view”>

            <field name=”model”>hospital.patient.search</field>

            <field name=”model”>hospital.patient</field>

            <field name=”arch” type=”xml”>

                <search>

                 

                 <field name=’name’ filter_domain=”[‘|’, ‘|’,(‘name’, ‘ilike’, self),

                         (‘age’, ‘ilike’, self), 

                         (‘gender’, ‘ilike’, self)]” 

                         string=’Patient_name’></field>

                 <field name=’age’></field>

                 <field name=’gender’></field>


 

                </search>

            </field>

        </record>





In the search tag, you can add various search fields and filters to define the search criteria. Here’s an example of how to add a search field for the name field of the model:


How To Add Filters And Group By Options In Odoo Search

 View || Odoo 16


In Odoo, you can add filters and group by options to your search view to make it more powerful and user-friendly. Here’s how to do it:


  1. Adding filters:
  2. To add filters, you can use the filter tag in your search view’s XML file. For example, if you want to add a filter for the name field of a model, you can use the following code:

record id=”view_hospital_patient_search” model=”ir.ui.view”>

            <field name=”model”>hospital.patient.search</field>

            <field name=”model”>hospital.patient</field>

            <field name=”arch” type=”xml”>

                <search>

                 

                 <field name=’name’ filter_domain=”[‘|’, ‘|’,(‘name’, ‘ilike’, self), (‘age’, ‘ilike’, self), (‘gender’, ‘ilike’, self)]” string=’Patient_name’></field>

                 <field name=’age’></field>

                 <field name=’gender’></field>


                 <filter name=”filter_male” string=”Male” domain=”[(‘gender’, ‘=’, ‘male’)]”></filter>


                 <filter name=”filter_female” string=”Female” domain=”[(‘gender’, ‘=’, ‘female’)]”></filter>


                 <filter name=”filter_kids” string=”Kids” domain=”[(‘age’, ‘&lt;=’, ‘5’)]”></filter>


                 <group expand=”0″ string=”Group By”>

                 <filter string=”Gender” name=”group_by_gender” context=”{‘group_by’ : ‘gender’}”></filter>

                 

                 </group>

                  


                 

                 

                      

                

                </search>

            </field>

        </record>






In this code, the string attribute specifies the label of the filter, the name attribute is the internal name of the filter, and the domain attribute specifies the search criteria for the filter.

  1. Adding group by options:
  2. To add group by options, you can use the groupby tag in your search view’s XML file. For example, if you want to add a group by option for the category_id field of a model, you can use the following code:

<group expand=”0″ string=”Group By”>

                 <filter string=”Gender” name=”group_by_gender” context=”{‘group_by’ : ‘gender’}”></filter>

                 

                 </group>



Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *