转自: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可能会不起作用

最新文章

  1. 深入浅出OOP(六): 理解C#的Enums
  2. Codeforces Round #337 Alphabet Permutations
  3. U盘启动
  4. Python核心编程--学习笔记--5--数字
  5. 11G RAC 简单命令
  6. 设计模式:Observer(观察者)—— Guava EventBus
  7. Light OJ 1021 - Painful Bases(状态压缩DP)
  8. System.InvalidOperationException: 无法加载协定为“ServiceReference1.XXXXXXXXXXXXXXXX”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。
  9. hdu 5422 Rikka with Graph(简单题)
  10. iOS 改变导航栏高度
  11. MVC无刷新查询,PagedList分页控件使用,导出Excel
  12. OC-不可变数组NSArray
  13. 给js动态创建的对象绑定事件
  14. Servlet,JSP 中的中文乱码问题以及解决方案
  15. select * 和 select 所有字段的区别
  16. 121、Data Binding(数据绑定)(转载)
  17. nginx屏蔽某段IP、某个国家的IP
  18. Redis中的简单动态字符串
  19. 将自己的域名解析跳转到博客主页(GitHub中的gitpage跳转)
  20. 如何确定一个NFS的mount是v3还是v4?

热门文章

  1. 简单说明CentOS源码安装程序
  2. 多协议注入工具t50
  3. 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)
  4. 2017icpc 乌鲁木齐网络赛
  5. HDOJ(HDU) 3949 XOR
  6. 【模拟】【set】hdu 4789 ICPC Ranking
  7. 连接mysql时1045错误的解决办法
  8. JS小游戏寻找房祖名
  9. HDU 4665 Unshuffle (2013多校6 1011 )
  10. golang解析json格式 -- 全