This question has been flagged
5 Replies
67826 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.

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

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/

Best Answer

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

Avatar
Discard
Author

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

Author

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

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

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/

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

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

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

Author 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.

Avatar
Discard

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?

Author

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.

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.

Best Answer

Hi,

Other than returning domain in onchange method, you can use the web domain field module from oca for the same:  Web Domain Field


See this: Return Dynamic Domain For Field In Odoo


How to use:

.. code-block:: xml

<field name="product_id_domain" invisible="1"/>
<field name="product_id" domain="product_id_domain"/>


.. code-block:: python

product_id_domain = fields.Char(
compute="_compute_product_id_domain",
readonly=True,
store=False,
)

@api.multi
@api.depends('name')
def _compute_product_id_domain(self):
for rec in self:
rec.product_id_domain = json.dumps(
[('type', '=', 'product'), ('name', 'like', rec.name)]
)

Returning domain from the onchange function: How To Give Domain For A Field Based On Another Field

Thanks

Avatar
Discard

as there is no api.multi in odoo v14 it is not work there it gives me error from js.

if you remove api.multi it will work in v14

Best Answer

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

Regards,

Avatar
Discard
Author

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