2015-10-14 65 views
2

我們需要在「新建」報價頁面中修改產品搜索行爲。我們需要加快速度,因爲我們有很多產品(超過1000萬)Odoo:如何在創建新報價時更改默認搜索行爲?

在「報價」頁面中,當用戶創建新報價並添加新訂單行並開始在「產品領域「,Odoo在用戶輸入時自動開始搜索。 Odoo在所有產品中進行搜索,並在用戶輸入「產品」字段時花費很長時間進行響應。

我重寫了sale.sale_order類的product_id_change方法並對其進行了更改。但是,它沒有工作!

爲了使產品查找速度更快,我希望Odoo只獲得與default_code等於在產品字段中輸入的值相等的產品。無需在其他字段中搜索產品名稱等。我怎樣才能做到這一點?

回答

2

您應該重寫name_search方法。檢查源代碼中的一些示例。這是product.template模型中的name_search方法。你應該在代碼中刪除或替換name領域:

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): 
    # Only use the product.product heuristics if there is a search term and the domain 
    # does not specify a match on `product.template` IDs. 
    if not name or any(term[0] == 'id' for term in (args or [])): 
     return super(product_template, self).name_search(
      cr, user, name=name, args=args, operator=operator, context=context, limit=limit) 
    template_ids = set() 
    product_product = self.pool['product.product'] 
    results = product_product.name_search(cr, user, name, args, operator=operator, context=context, limit=limit) 
    product_ids = [p[0] for p in results] 
    for p in product_product.browse(cr, user, product_ids, context=context): 
     template_ids.add(p.product_tmpl_id.id) 
    while (results and len(template_ids) < limit): 
     domain = [('product_tmpl_id', 'not in', list(template_ids))] 
     results = product_product.name_search(
      cr, user, name, args+domain, operator=operator, context=context, limit=limit) 
     product_ids = [p[0] for p in results] 
     for p in product_product.browse(cr, user, product_ids, context=context): 
      template_ids.add(p.product_tmpl_id.id) 


    # re-apply product.template order + name_get 
    return super(product_template, self).name_search(
     cr, user, '', args=[('id', 'in', list(template_ids))], 
     operator='ilike', context=context, limit=limit)  

您可能需要做同樣的product.product模型。

我希望這有助於你

0
@api.multi 
    def name_get(self): 
     if not len(self.ids): 
      return [] 
     resuhh = [] 
     product_name = [] 
     for record in self: 
      if (record.ref) and (record.job): 
       product_name = '[' + str(record.job) + ']' + '[' + str(record.ref) + ']' 
       product_name += str(record.name) 
       s = resuhh.append((record.id, product_name)) 
      elif record.ref: 
       product_name = '[' + str(record.ref) + ']' 
       product_name += str(record.name) 
       s = resuhh.append((record.id, product_name)) 
      elif record.job: 
       product_name = '[' + str(record.job) + ']' 
       product_name += str(record.name) 
       s = resuhh.append((record.id, product_name)) 

      else: 
       s = resuhh.append((record.id, record.name)) 

     return resuhh 

    @api.model 
    def name_search(self, name, args=None, operator='ilike', limit=100): 
     args = args or [] 
     recs = self.browse() 
     if name: 
      recs = self.search((args + ['|', ('ref', 'ilike', name), ('job', 'ilike', name)]), 
           limit=limit) 
     if not recs: 
      recs = self.search([('name', operator, name)] + args, limit=limit) 
     return recs.name_get() 
相關問題