• 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可能会不起作用

  • 相关阅读:
    md5盐加密
    springcloud alibaba文档
    http header, url参数转json
    Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000018DC4F2C520>: Failed to establish a new connection:
    Nacos配置(配置中心)
    java.lang.NoSuchFieldError: VERSION_2_3_0 报错解决方案
    Cannot resolve freemarker:freemarker:2.3.23
    springcloud nacos config 配置application.yml
    可选链操作符(?.)
    浏览器network
  • 原文地址:https://www.cnblogs.com/chjbbs/p/4056514.html
Copyright © 2020-2023  润新知