Search code examples
xmlviewfilteringodoosearchview

Disable updating filter in Odoo


I have create a search view and define my filter in it:

<record id="dept_search_view" model="ir.ui.view">
<field name="name">employee.search.view</field>
 <field name="model">employee</field>
<field name="arch" type="xml">
    <search string="dept_search">
        <filter string="Department" name="dept_search" domain="[('e_dept', 'in', ['HR','Audit'])]">

        </filter>

    </search>
</field>

I added the filter to the view in which i want to apply the filter by default.

<record model="ir.ui.view" id="view_employee_tree">
    <field name="name">employee.tree</field>
    <field name="model">employee</field>
   <field name="context">{"search_default_dept_search":1}</field>
    <field name="priority" eval="8" />
    <field name="arch" type="xml">
        <tree string="Employee">
            <field name="e_dept"/>
            <field name="employee_id"/>
            <field name="employee_name"/>
        </tree>
    </field>
</record>

But, the user can still remove the default filter in the search bar. How can I prevent the user to remove the filter?


Solution

  • By default odoo allow you to remove filter in the search view.

    From what you said i think that you don't need the user to see other records. you can do this by two technique the easy one easy use the action definition

      <record id="your_action_id" model="ir.actions.act_window">
           ....
           ....
           ....
           ....
           <!-- in your action definition just add a domain like this -->
           <field name="domain">[('e_dept', 'in', ['HR','Audit'])]</field>
      </record>
    

    In this technique that odoo provide all your user when they hit the menu that execute this action they will see only the records that match the domain. if this is what you want use it.

    Sometime you want to keep the same menu but for group of users you want them to see only the records that match the domain but for other group of user like administrators you want them to see all records. you need to do this using ir.rule.

    Let say that the group of users that you want them to see only record that match your domain is : 'hr_audi_group'

     <record model="ir.rule" id="hr_audi_group_rule">
            <field name="name">Show only 'HR' and 'Audit' record for hr_audi_group</field>
            <field name="model_id" ref="model_employee"/>
            <field name="domain_force">[('e_dept', 'in', ['HR','Audit'])]</field>
             <field name="perm_read" eval="True"/>
            <field name="perm_create" eval="True"/>
            <field name="perm_write" eval="True"/>
            <field name="perm_unlink" eval="True"/>
            <-- this rule will be applied only for users that belong to this group -->
            <field name="groups" eval="[(4,ref('hr_audi_group')]"/>
        </record>
    

    So here in the same menu for some users it will show all records but for user with that belong to hr_audi_group they will only see part of the records.

    Hope that helps you.