Odoo Help


Conditionally apply Domain on a field

Shaumyadeep Chaudhuri
on 7/22/13, 9:59 AM 10,572 views

Is it posssible to apply a domain only when some condition is true otherwise the domain should not be applied in V7

For eg. in a many2one field to hr.employee, the manager should be able to see only the employee under him but someone from the hr department should be able to view all the employees in the view.

No one know how to do it? is it even possible?

Shaumyadeep Chaudhuri
on 7/24/13, 11:02 AM

I solved the problem I couldn't find a regular way to do what i wanted. I feel this is a bit hackish way to achieve what i wanted; what i did was override the fields-view_get method and implemented what i wanted in that. here's a sample code of what i did, maybe it'll help other looking to implement similar functionality

def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):

    Overrides orm field_view_get.
    @return: Dictionary of Fields, arch and toolbar.

    res = {}
    res = super(employee_exit, self).fields_view_get(cr, user, view_id, view_type,
                                                   context, toolbar=toolbar, submenu=submenu)

    hr_employee = self.pool.get('hr.employee')
    emp = hr_employee.search(cr, user, [('user_id', '=', user)])[0]
    logged_employee = hr_employee.browse(cr, user, emp)
    from lxml import etree
    for node in doc.xpath("//field[@name='target_employee_id']"):
        if not logged_employee.department_id.name == "Human Resources":        
            node.set('domain', "['&','|',('parent_id.user_id', '=', uid),('department_id.manager_id.user_id','=',uid),('user_id','!=',uid)]")
        elif logged_employee.department_id.manager_id.user_id.id==user:
            node.set('domain', "[('user_id','!=',uid)]")
            node.set('domain', "['&','&',('user_id','!=',uid),('user_id','!=',"+str(logged_employee.department_id.manager_id.user_id.id)+\
    return res

EDIT: I can't mark this as the answer as i don't have sufficient points. if somebody finds it useful and doesn't know of any better method please mark this as the answer so it may be visible to others in need at the top


Daniel Reis

--Daniel Reis--
| 7 8 9
Linda-a-Velha, Portugal
--Daniel Reis--

Author of the "Odoo Development Essentials" book.

Applications Manager at Securitas Portugal

Github: https://github.com/dreispt

Twitter; @reis_pt

Daniel Reis
On 7/24/13, 2:37 PM

Funny enough, the question and the example provided can have two different answers:

Is it possible to have a a "conditional" domain?

Yes, by building the domain in a smart way, using OR (|) operators. For example, on a particular Model having a company_id field, you could use:


Here the "child of" condition will only be enforced if the record's "company_id" has a value.

The manager should see only his employees, but hr department should see all

To easiest way to achieve this is to have two user Groups. The Managers group has a record rule to view only his employees and the HR manager group has a record rule to see all employees. You can find a similar example in the CRM app: there is a "Sales / See Own Leads" group and a "Sales / See All Leads" group.

Record rules apply to current record based on User attributes. You would need to know the Department to use for the current User, but AFAIK that info is not available in the User model. You would somehow need to have a department_id in the User and then could create a record rule (or many2one domain) similar to:


Thanks, that was educational but i have a many2one field to hr.employee which is being displayed in the view obviously as a dropdown, now what i want is if the logged in user has department the HR then when he uses the drop down he should be able to see all the employees of the other departments and all the employees below him in HR Department, but if the logged in user is a not of the HR Department and uses the dropdown he should be able to see only the employees he the parent of. Also please explain in the above example which company_id do you mean the user.company_id or 'company_id'

Shaumyadeep Chaudhuri
on 7/25/13, 3:45 AM

Expanded the answer. The feature you need would ge a great addition to lp:openerp-hrms.

Daniel Reis
on 7/25/13, 6:41 AM

So in short its not posssible to get exactly what i need with exisiting functionality?

Shaumyadeep Chaudhuri
on 7/25/13, 6:58 AM

res.user doesnt have any field like department_id.... how is it possible to use this?....i have got error.....

Umashankar Subramani
on 2/12/15, 7:30 AM

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

1 follower(s)


Asked: 7/22/13, 9:59 AM
Seen: 10572 times
Last updated: 3/16/15, 8:10 AM