Search code examples
python-2.7many-to-manyodooodoo-9many-to-one

Odoo error: return self.models[model_name] KeyError: 'res_groups_users_rel'


I need to make UI many2one dopdown list where I can identify users which depend to Manager group role.

Now I have dropdown field:

test = fields.Many2one('res.groups', 'Purchase request type', default=_get_users, track_visibility='onchange')

And I tried to write a function which can identify all users which depend to manager group role.

def _get_users(self):
    pickings = self.env['res_groups_users_rel'].search([('gid','=',61)])
    pickings_available = []
    for picking in pickings:
            pickings_available.append(picking)
    return pickings_available

And I got an error:

    return self.models[model_name]
KeyError: 'res_groups_users_rel'

I don't know how can I change this function and get value from amy2many relation.

I changed my function to:

def _get_users(self): pickings = self.env['res.groups'].browse(61).users pickings_available = [] for picking in pickings: pickings_available.append(picking) return pickings_available

and field:

test = fields.Many2one('res.users', 'Some text', default=_get_users, track_visibility='onchange')

I logged function _get_users and get values: [res.users(9,), res.users(65,)]

But I still can't get these values on my test field dropdown. What I am doing wrong?


Solution

  • If you are trying to get all users that belong to a group, why not do the following:

    self.env['res_groups'].browse(61).users
    

    On a side note, you might get an error, trying to assign a list as default value to a Many2one field.

    Also you seem to be assigning users belonging to a group to a field that is specified to store reference to groups.

    If you need to have a field to select a user that belongs to group with id 61, you can do the following:

    test = fields.Many2one('res.users', 'Some description', domain="[('groups_id', 'in', [61])]")