Wednesday, 22 November 2017

Show records on TreeView that are related to Active User

In Odoo to show records that are related to Active User is not a big deal. First understand what we are going to do here.

Problem Statement

Let say we have a company and in that company we have different active user related to different department. And we want to display some data or records in a tree view or list view according to active user department, when some one clicks on related menu. To achieve this goal we need to create dynamic domain on action window.

Solution

To put domain on action window we need a computed filed in Odoo 8 or function field in older version of Odoo, in that computed field we need to create two method first one for computed field and second one for search.

       

    department_clo_acl_ids=fields.Char(compute="_compute_department_clo_acl_ids",search=' department_clo_acl_ids_search')
    @api.one
    @api.depends('department_id')
    def _compute_department_clo_acl_ids(self):
        print('View My Department CLO ACL')

    def department_clo_acl_ids_search(self, operator, operand):
        clo_acl_obj = self.env['obe.clo.acl'].search([('department_id','=',self.env.user.faculty_id.department_id.id)]).ids
        return [('id','in',clo_acl_obj)]
       
 

Code Description

In above code we have created a computed field, and in computed field we used search method. Notice search method takes three parameter self, operator and operand. And in that method we return records that are related to active user department.

Usage of computed field as domain in action window

To display records related to active user we need to put this computed field "department_clo_acl_ids" in action window below view_mode as a domain.

       <field name="view_type">form</field>
       <field name="view_mode">tree,form</field>
       <field name="domain">[('department_clo_acl_ids','=',1)]</field> 

1 comment: