Odoo Help


Tree filter dynamic for department

edgard pimentel rojas
on 10/20/13, 2:47 AM 2,627 views

I just introduced a new problem now, the problem is as follows. I have a view (tree), which has the following columns:

department name description

I want is to filter the tree I agree to the department that I am logged in as user.

For example if I am logged in as user1, which belongs to the logistics department, I need to filter by department logistics.

What I did through a search, you agrege one domain (domain = logistics), which works but I need to be dynamic as I need to be agree to meet me logged department at that time.


Brett Lehrer

--Brett Lehrer--

| 5 5 8
Milwaukee, United States
--Brett Lehrer--
Director of IT and open source enthusiast.
Brett Lehrer
On 10/21/13, 10:41 PM

You could try extending the search function for that object:

from openerp.osv import osv, fields

class my_class(osv.osv)
    _name = 'my.class'
    # [normal class code here...]

    def search(cr, uid, domain, offset=0, limit=None, order=None, context=None, count=False):
        if context is None: context = {}

        # find the user's department (an example, I don't know your code)
        user_rec = self.pool.get('res.users').browse(cr, uid, uid, context)
        dept_id = user_rec.user_department.id

        # Append the domain to the existing one, and return the original search function
        domain += [('department_id','=',dept_id)]
        return super(my_class, self).search(cr, uid, domain, offset, limit, order, context, count)
edgard pimentel rojas
On 10/22/13, 2:48 PM

thanks so much,

I add my code for you to see and if run.

def search(self, cr, uid, ids,offset=0, limit=None, order=None, context=None, count=False):
    if context is None:
        context = {}

    pool_helper = self.pool.get('document.helper')
    department = pool_helper.get_department(cr, uid, ids, context=context)
    domain = [('dpt_dest', '=', department)]
    return super(documentary_step_move, self).search(cr, uid, domain, offset, limit, order, context, count)

As long as get_department() is returning the id of the department record, it looks fine to me.

Brett Lehrer
on 10/22/13, 9:05 PM

that's right return name of department

def get_department(self, cr, uid, ids, context=None):

     pool_resource = self.pool.get('resource.resource')
     pool_employee = self.pool.get('hr.employee')

     resource_id = pool_resource.search(cr, uid, [('user_id', '=', uid)])
     employee_id = pool_employee.search(cr, uid, [('resource_id', '=', resource_id)])
     employee = pool_employee.browse(cr, uid, employee_id, context=context)

     department = ''
     for obj_employee in employee:
         department =  obj_employee.depar
edgard pimentel rojas
on 10/25/13, 1:21 PM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 10/20/13, 2:47 AM
Seen: 2627 times
Last updated: 3/16/15, 8:10 AM