Odoo Help

Welcome!

This community 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.

2

How to use dynamic values in domain filter?

By
vivek
on 5/18/13, 5:31 AM 18,125 views

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.

3
vivek
On 5/22/13, 2:16 AM

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.

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?

Obay Albadri
on 6/24/13, 7:51 AM

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.

vivek
on 8/7/13, 4:34 AM

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.

bala
on 9/3/14, 7:51 AM
7

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 5/18/13, 8:41 AM

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

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!!

vivek
on 5/20/13, 4:57 AM

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

Sandeep
on 5/20/13, 5:28 AM

:) 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.

vivek
on 5/20/13, 5:35 AM

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 ......:)

Sandeep
on 5/20/13, 5:38 AM

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

Obay Albadri
on 6/24/13, 7:43 AM

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/

evon_dun
on 9/19/13, 3:25 AM

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

vimal j
on 2/27/14, 2:12 AM

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

vimal j
on 2/27/14, 2:13 AM

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

vimal j
on 2/27/14, 2:17 AM
1
Cristian Salamea
On 5/20/13, 1:37 AM

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

Regards,

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

vivek
on 5/20/13, 2:10 AM

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

Cristian Salamea
on 5/20/13, 2:21 AM
1

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 5/18/13, 7:21 AM

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.

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

vivek
on 5/18/13, 7:27 AM

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/

evon_dun
on 9/19/13, 3:21 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/18/13, 5:31 AM
Seen: 18125 times
Last updated: 10/11/16, 7:44 PM