Help

3

How to use dynamic values in domain filter?

Avatar
vivek

Hi,

The following is my line of code and is working,

'user_id': fields.many2one('res.users', 'Responsible', track_visibility='onchange', domain="[('emp_department_id', '=', 10)]"),

Now i want replace that employee department id and that should be the value returned by a function,

say something similar to this

def test():
     return department id of the login users

'user_id': fields.many2one('res.users', 'Responsible', track_visibility='onchange', domain="[('emp_department_id', '=', self.test())]"),

I know the above statement is not valid since the whole thing is a dict and domain is one of the key in the dict and its value is string. I want this concept to be done somehow.

I am even ok for some crooked ideas. Thanks for your time.

Avatar
Discard
4 Answers
3
Avatar
vivek
Best Answer

Hi,

The below is working for me to give dynamic values for domain filters for a particular field.

I've placed the below code in fields_view_get() method.

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    login_user_dpt_id = users_obj.browse(cr, SUPERUSER_ID, uid, context=context).emp_department_id
    for node in eview.xpath("//field[@name='user_id']"):
        if login_user_dpt_id:
            user_filter =  "[('emp_department_id', '='," + str(login_user_dpt_id.id) + " )]"
            node.set('domain',user_filter)

Thanks for all your replies and time.

3 Comments
Avatar
Discard
Avatar
Obay Albadri
-

vivak, i had a long path to git to this and now my problem start from here, what if 'user_filter' depends on other field value in the same form (eg. 'status')?, how i can get it?

Avatar
vivek
-

In python, you can access all the fields' values obay. And so u use them in the place where i have login_user_dpt_id and set something like this st = self.browse(cr,uid,id,context) and then your operation and finally the conditino like this user_filter = "[('emp_department_id', '='," + str(your final constructed condition) + " )]". Please check and clarify. Sorry for late reply as i didn't notice it.

Avatar
bala
-

Hi vivek, I am from Madurai. i have also same problem. could you explain briefly? I tried your code which you posted. Nothing happening. pls help.

8

Hey vivek

just do like below

remove domain from below field (.py), like this

'user_id': fields.many2one('res.users', 'Responsible'),

for dynamic domain , you should have an another field

in view code will be like this :

 <field name="myfirst_field" on_change="get_domain_useer_id()" />
 <field name="user_id" />

after this you should add a function in your model object

  def get_domain_useer_id(self,cr,uid,ids,context=None):
    mach=[]
    lids=self.pool.get('res.users').search(cr,uid,[('active','=',True)])
    return {'domain':{'user_id':[('id','in',lids)]}}

This is just example to see all active user for dynamic domain you can add more effective code as you want

good luck

Thanks

9 Comments
Avatar
Discard
Avatar
vivek
-

Thanks a good idea sandeep, but what happens if the users has not changed the first field or in my case i do not have any first field to select. I just having the user field for selection and values must not be available even before doing anything in the form. Thanks!!

if you don't have first field then , where is a need of dynamic domain,,,,,, i think then no need of dynamic domain

Avatar
vivek
-

:) yeah i don't need a dynamic domain, i have to use dynamic values in the domain filter. as i've narrated in the ques i want to take the login users department id and place them in the domain filter. That should be applicable for the values when loading the form itself. I am open for clarification.

we can get login user directly , you know uid just put uid in domain but without method we can't get his depratment id ,, give me time ..... what can i do for you ......:)

Avatar
Obay Albadri
-

but in domain we can't just say: domain = "[('emp_department_id', '=', uid.department_id)]" ?!

Avatar
evon_dun
-

Can we add date range fields and print its value in the reports too? I added two fields date_from and date_to and tried to print its value. Please help if someone knows. Here is the link to my question http://help.openerp.com/question/28218/how-to-use-data-of-a-date-range-from-search-view-in-my-openoffice-report/

Avatar
vimal j
-

why did u create mach=[] in function get_domain_useer_id.

Avatar
vimal j
-

why did u add mach=[] in function get_domain_useer_id? Is there any specific reason for that?

Avatar
vimal j
-

why did u add mach=[] in function get_domain_useer_id? Is there any specific reason for that?

1
Best Answer

You can redefine fields_view_get method and build your own domain and set it on view modifying with lxml.

Regards,

2 Comments
Avatar
Discard
Avatar
vivek
-

Thanks for your reply cristian, is it possible to give some reference links?

Hi, you can read the doc here: http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html and some example in code: http://bazaar.launchpad.net/~ocb/ocb-addons/6.1/view/head:/account/account_invoice.py#L337

1

You can use on_change. Write a onchange on emp_department_id and in that onchange method you can return domain for your user_id field.

Have a look at the example of onchange which returns value, domain and warning.

2 Comments
Avatar
Discard
Avatar
vivek
-

Thanks sudhir, It's a one2many field(res.users) i've used in project -> task form. All the users are listed there and i want to show users whose department is equal to the login person's department. Hope you exactly understand what i want and guide me to do that. Expecting your reply hopefully. Thanks

Avatar
evon_dun
-

Can we add date range fields and print its value in the reports too? I added two fields date_from and date_to and tried to print its value. Please help if someone knows. Here is the link to my question http://help.openerp.com/question/28218/how-to-use-data-of-a-date-range-from-search-view-in-my-openoffice-report/