title: How do I narrow a search in res.user to users that belong to the same group I do in OpenERP?
body: <p>I am implementing a module on OpenERP 7.0 to manage requests. Each request may (or may not) have an owner. The owner of a request belongs to some group and in a given moment he might want to give away his request to some other user in the same group. My question goes precisely in that direction. Having to select a user from res.users, is there a way to narrow his search possibilities to the users that belong to the same group he does?</p>
  <hr />
  <p>Adding a little more info:</p>
  <p>I have 4 groups set up in OpenERP: User, HR Group, Logistics, and Manager
  In some stage of processing, only users belonging to the HR group (and obviously Manager group) can take action on requests. As I said before, users of HR group make take responsability of a requests becoming owners of it, at this stage, only the owner can make changes on this request. He may, however, need to give the ownership of this request to some other user, belonging to the same HR group. In order to do that, I am opening a wizard where he has to select a user from res.users. Since I will have about 4k users, it would be nicer to narrow his search to the users with the same permissions (also belonging to OpenERP's group created in this module).</p>
  <p>Here is the python code of my wizard:</p>
  <pre><code>from openerp.osv import osv
  from openerp.osv import fields
  from openerp.tools.translate import _
  import netsvc
  
  class give_request(osv.osv_memory):
      _name='give.request'
  
      _columns = {
          'assign_to': fields.many2one('res.users', 'Assign request to', help='Person who will be responsible for the next action'), 
          }
      
      def save_info(self, cr, uid, ids, context=None):
          if 'active_id' in context:
              assign_to=self.browse(cr,uid,ids)[0].assign_to
              #import pdb; pdb.set_trace()
              self.pool.get('generic.request').write(cr,uid,context['active_id'],{'assign_to' : assign_to.id})
              wf_service = netsvc.LocalService('workflow')
              wf_service.trg_validate(uid,'generic.request',context['active_id'],'give_request',cr)    
          return {
                  'type': 'ir.actions.act_window_close',
           }
  
  give_request()
  </code></pre>
  <p>And the XML view:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;openerp&gt;
      &lt;data&gt;
  
          &lt;!-- Give request --&gt;
          &lt;record id=&quot;view_give_request_wizard&quot; model=&quot;ir.ui.view&quot;&gt;
              &lt;field name=&quot;name&quot;&gt;give_request_wizard.form&lt;/field&gt;
              &lt;field name=&quot;model&quot;&gt;give.request&lt;/field&gt;
              &lt;field name=&quot;type&quot;&gt;form&lt;/field&gt;
              &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;
                  &lt;form string=&quot;Select user&quot; version=&quot;7.0&quot;&gt;
                      &lt;group &gt;
                          &lt;separator string=&quot;Please select the person you want to set the ownership of the request&quot; colspan=&quot;2&quot;/&gt;
                          &lt;!--&lt;field name=&quot;assign_to&quot; string=&quot;Assign to&quot; domain=&quot;[('groups_id', 'in', [uid.groups_id])]&quot; /&gt;--&gt;
                          &lt;field name=&quot;assign_to&quot; string=&quot;Assign to&quot;  /&gt; 
                          &lt;newline/&gt;
                      &lt;/group&gt;
                      
                      &lt;div style=&quot;text-align:right&quot;&gt;
                          &lt;button  icon=&quot;gtk-cancel&quot; special=&quot;cancel&quot; string=&quot;Cancel&quot;/&gt;
                          &lt;button  icon=&quot;gtk-ok&quot; name=&quot;save_info&quot; string=&quot;Give the request&quot; type=&quot;object&quot; /&gt; 
                      &lt;/div&gt;
                 &lt;/form&gt;
              &lt;/field&gt;
          &lt;/record&gt;
  
          &lt;record id=&quot;action_give_request&quot; model=&quot;ir.actions.act_window&quot;&gt;
              &lt;field name=&quot;name&quot;&gt;Give ownership of the request&lt;/field&gt;
              &lt;field name=&quot;type&quot;&gt;ir.actions.act_window&lt;/field&gt;
              &lt;field name=&quot;res_model&quot;&gt;give.request&lt;/field&gt;
              &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;
              &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;
              &lt;field name=&quot;view_id&quot; ref=&quot;view_give_request_wizard&quot;/&gt;
              &lt;field name=&quot;target&quot;&gt;new&lt;/field&gt; 
          &lt;/record&gt;
  
          &lt;act_window id=&quot;action_give_request&quot;
                  name=&quot;Give request&quot;
                  res_model=&quot;give.request&quot; 
                  view_mode=&quot;form&quot; 
                  target=&quot;new&quot;
          /&gt;
  
      &lt;/data&gt;
  &lt;/openerp&gt;
  </code></pre>
  <p>I tried to apply a domain filter on my assign_to filter <code>&lt;field name=&quot;assign_to&quot; string=&quot;Assign to&quot; domain=&quot;[('groups_id', 'in', [user.groups_id])]&quot; /&gt;</code> (and later with <code>uid.groups_id</code>), but it didn't work.</p>
  <p>Thanks in advance.</p>
  <hr />
  <p>New edit, to adapt my code to nitesh's hints.</p>
  <p>Now the python code of my wizard looks like this:</p>
  <pre><code>class give_request(osv.osv_memory):
      _name='give.request'
  
      def search(self, cr, uid, domain, offset=0, limit=None, order=None, context=None, count=False):
          cr.execute(&quot;SELECT gid FROM res_groups_users_rel WHERE uid=&quot;+str(uid))
          group=cr.fetchall()
          names = []
          #import pdb; pdb.set_trace()
          for group_id in group:
              cr.execute(&quot;SELECT name FROM res_groups WHERE id=&quot;+str(group_id[0]))
              group_name = cr.fetchall()
              names.append(group_name[0][0])
          #Now &quot;names&quot; will contain al the group that the user has assigned to
          #Check if &quot;Hr Group&quot; is present in that group or not
          if 'UC Human Resources' in names:
              domain += [('group','=','UC Human Resources')]
          
          return super(give_request, self).search(cr, uid, domain, offset, limit, order, context, count)
  
      _columns = {
          #'assign_to': fields.many2one('res.users', 'Assign request to', help='Person who will be responsible for the next action'), 
          'assign_to': fields.function(search, 'Assign request to', help='Person who will be responsible for the next action', type=&quot;integer&quot;, obj=&quot;res.users&quot;,  method=True),
  
          }
      
      def save_info(self, cr, uid, ids, context=None):
          if 'active_id' in context:
              assign_to=self.browse(cr,uid,ids)[0].assign_to
              #import pdb; pdb.set_trace()
              self.pool.get('generic.request').write(cr,uid,context['active_id'],{'assign_to' : assign_to.id})
              wf_service = netsvc.LocalService('workflow')
              wf_service.trg_validate(uid,'generic.request',context['active_id'],'give_request',cr)    
          return {
                  'type': 'ir.actions.act_window_close',
           }
  
  
  give_request()
  </code></pre>
  <p>And didn't make any changes to my XML, since I'm not sure if it is needed or not.</p>
  <p>Anyway, with the change I've done, now XML is displayed like this<img src="https://i.stack.imgur.com/QZuTr.png" alt="enter image description here" />
  (With a &quot;0&quot; in front of the field of a dropdown, wich is not what I expected, I hoped to get a dropdown here...)
  And another strange thing is that even thou I added pdb inside the definition of search function it doesn't stop when the wizard loads (shouldn't it stop, since it is called by the field.function?) it stops when I click the save button I guess the <code>assign_to=self.browse(cr,uid,ids)[0].assign_to</code> line of the save_info function calls it (?) and then, when I do the debug, I guess I get an infinite loop.</p>
  <p>Otherwise, without the pdb, I get the following trace (after I click the save button):</p>
  <pre><code>2014-06-11 14:19:08,063 753 ERROR may_9 openerp.osv.osv: Uncaught exception
  Traceback (most recent call last):
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 131, in wrapper
      return f(self, dbname, *args, **kwargs)
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 197, in execute
      res = self.execute_cr(cr, uid, obj, method, *args, **kw)
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 185, in execute_cr
      return getattr(object, method)(cr, uid, *args, **kw)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 3606, in read
      result = self._read_flat(cr, user, select, fields, context, load)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 3726, in _read_flat
      res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
    File &quot;/opt/openerp/v7/server/openerp/osv/fields.py&quot;, line 1133, in get
      result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
    File &quot;/home/lfc/openerp/v7/addons/processos_uc/wizard/give_req_wiz.py&quot;, line 23, in search
      return super(give_request, self).search(cr, uid, domain, offset, limit, order, context, count)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 2356, in search
      return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 4847, in _search
      query = self._where_calc(cr, user, args, context=context)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 4676, in _where_calc
      e = expression.expression(cr, user, domain, self, context)
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 642, in __init__
      self.parse(cr, uid, context=context)
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 742, in parse
      self.stack = [ExtendedLeaf(leaf, self.root_model) for leaf in self.expression]
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 528, in __init__
      self.check_leaf()
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 584, in check_leaf
      raise ValueError(&quot;Invalid leaf %s&quot; % str(self.leaf))
  ValueError: Invalid leaf 16
  2014-06-11 14:19:08,068 753 ERROR may_9 openerp.netsvc: Invalid leaf 16
  Traceback (most recent call last):
    File &quot;/opt/openerp/v7/server/openerp/netsvc.py&quot;, line 292, in dispatch_rpc
      result = ExportService.getService(service_name).dispatch(method, params)
    File &quot;/opt/openerp/v7/server/openerp/service/web_services.py&quot;, line 626, in dispatch
      res = fn(db, uid, *params)
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 188, in execute_kw
      return self.execute(db, uid, obj, method, *args, **kw or {})
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 131, in wrapper
      return f(self, dbname, *args, **kwargs)
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 197, in execute
      res = self.execute_cr(cr, uid, obj, method, *args, **kw)
    File &quot;/opt/openerp/v7/server/openerp/osv/osv.py&quot;, line 185, in execute_cr
      return getattr(object, method)(cr, uid, *args, **kw)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 3606, in read
      result = self._read_flat(cr, user, select, fields, context, load)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 3726, in _read_flat
      res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
    File &quot;/opt/openerp/v7/server/openerp/osv/fields.py&quot;, line 1133, in get
      result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
    File &quot;/home/lfc/openerp/v7/addons/processos_uc/wizard/give_req_wiz.py&quot;, line 23, in search
      return super(give_request, self).search(cr, uid, domain, offset, limit, order, context, count)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 2356, in search
      return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 4847, in _search
      query = self._where_calc(cr, user, args, context=context)
    File &quot;/opt/openerp/v7/server/openerp/osv/orm.py&quot;, line 4676, in _where_calc
      e = expression.expression(cr, user, domain, self, context)
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 642, in __init__
      self.parse(cr, uid, context=context)
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 742, in parse
      self.stack = [ExtendedLeaf(leaf, self.root_model) for leaf in self.expression]
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 528, in __init__
      self.check_leaf()
    File &quot;/opt/openerp/v7/server/openerp/osv/expression.py&quot;, line 584, in check_leaf
      raise ValueError(&quot;Invalid leaf %s&quot; % str(self.leaf))
  ValueError: Invalid leaf 16
  </code></pre>
  <p>Not being an expert on python and everyday that passes realising not an expert on OpenERP either, I don't know what I'm doing wrong? Anyone?
  Thanks in advance!</p>