首页 > 代码库 > name_search方法的使用

name_search方法的使用

转自:http://blog.csdn.net/littlebo01/article/details/22075573

在many2one类型中,页面下拉时会首先触发name_search方法,参数这里就不解释了

优化前:

def name_search(self, cr, user, name, args=None, operator=ilike, context=None, limit=100):              if not args:                  args = []              if context.has_key(current_id):                  current_pid = context[current_id]                  cr.execute(select id from stock_picking_apply_move)                  ids_get = cr.fetchall()                  ids = []                  if not ids_get:                      pass                  else:                      for id in ids_get:                          ids.append(id[  0])                  moves_obj = self.pool.get(stock.picking.apply.move).browse(cr, user, ids, context=context)                  pro_ids = []                  for move in moves_obj:                      if move.jp_apply_picking_id.id==current_pid:                          pro_ids.append(move.product_id.id)                  return self.name_get(cr, user, pro_ids, context=context)              else:                  #super(jp_product_product,self).name_search(cr, user, name, args=args, operator=operator, context=context, limit=limit)                  tenant_id = context[tenant_id]                  if not tenant_id:                      raise except_osv(_(warning),_(必须选择租户))                  if context.has_key(tenant_id):                      cr.execute(select id from product_product where tenant_id = %s,(tenant_id,))                      ids_get = cr.fetchall()                      ids = []                      if not ids_get:                          return {}                      else:                          for id in ids_get:                              ids.append(id[  0])                          return self.name_get(cr, user, ids, context=context)                  else:                      raise except_osv(_(warning),_(必须选择租户))                               def name_get(self, cr, uid, ids, context=None):              """Get Product In Picking"""              if not len(ids):                  return []              res = [ (r[id], r[name] and r[name]                                            or r[name] )                      for r in self.read(cr, uid, ids, [name, id],                                         context=context) ]              return res

优化后:

def name_search(self, cr, user, name, args=None, operator=ilike, context=None, limit=100):          if context.has_key(current_id):              current_pid = context[current_id]                ids = []              apply_obj = self.pool.get(stock.picking.apply).browse(cr, user, current_pid, context=context)              for move_line in apply_obj.move_lines_apply:                  prod = move_line.product_id                  ids.append(move_line.product_id.id)                return self.name_get( cr, user, ids, context=None)            elif context.has_key(tenant_id):              if context[tenant_id] == False:                  raise except_osv(_(提示:),_(请选择租户))              args = [(tenant_id, =, context[tenant_id])]            return super(jp_product_product,self).name_search(cr, user, name, args=args, operator=operator, context=context, limit=limit)

有没有发现,差异很大呢。

注意:使用了name_search方法,在xml中加的domain可能会不起作用

name_search方法的使用