首页 > 代码库 > 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方法的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。