This question has been flagged
2 Replies
9878 Views

Hello everyone,

I have a problem I am unable to fix on Odoo v12.

I have a many2one field related to "res.users" model, and need to show on this list, only the users that belongs to a specific group.

Searched for several related questions and none helped me to achieve what I need.

I have setup my groups as:
    - module_group_admin;
    - module_group_user; -----> I need to list these users
   
At this stage my groups/permissions are all working as expected.
The only problem I am having is that, when I access this many2one field, I see all users on the system and need to list ONLY the users from a specific and pre-selected group.

Can anyone help please?

Thank you in advance

Avatar
Discard
Best Answer

Hello Paulo,

This can be done by on-change function.

@api.multi
@api.onchange('company_id')
def onchange_company_id(self):
users = get the users from the both groups
return {'domain': {'your many2one field': [('id', 'in', users.ids)]}}

 I given this onchange based on company_id, every model having company field and it's having default value so i preferred  company_id field (you can do it with using another field also)


Avatar
Discard
Author

@Hilar and @subbarao, once again thank you very much for your clean explanation.

I have been learning a lot with both of you.

Best Answer

Hi Paulo Matos,

This can be done in onchange function of many2one field. eg:

1. find the user ids in onchange of the many2one field.

users = self.env.ref('group_name').users.ids

2. return domain to the many2one field with these ids.

return {'domain': {'field_name': [('id', 'in', users)]}}

3. full function structure.

@api.onchange('many20ne_field_name')
def onchange_many2onefield(self):
"""

:param self:
:return:
"""
users = self.env.ref('group_name').users.ids
return {'domain': {'field_name': [('id', 'in', users)]}}

This logic can be used for onchange with other fields in the same model or do changes accordingly.


Avatar
Discard