From bf31ab6718a8b9c8afdd29120e5056533b0a2ade Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 25 Feb 2015 17:46:38 +0100 Subject: [PATCH] [FIX] product.template: name_search compatibility with product.product In the rare places with a m2o field to product.template (e.g BoMs), it is necessary to be able to search on product codes, EAN, etc, the way it works on product.product. This is done by delegating the name_search to product.product and then returning the corresponding templates. This has a small penalty of executing name_get() twice, but is simpler and more robust for future changes. An alternative would have been to extract the name_search() method into a mixin and mix it on both product.product and product.template. However this would be more brittle and only work as long as the name_search implementation strictly uses fields that are present in both tables. OPW 626662 --- addons/product/product.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/addons/product/product.py b/addons/product/product.py index 5e0abcc691a..95e21eac7a7 100644 --- a/addons/product/product.py +++ b/addons/product/product.py @@ -22,8 +22,10 @@ import math import re import time +from collections import OrderedDict from _common import ceiling + from openerp import SUPERUSER_ID from openerp import tools from openerp.osv import osv, fields, expression @@ -797,9 +799,16 @@ class product_template(osv.osv): pass return super(product_template, self).name_get(cr, user, ids, context) - - - + def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): + 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] + template_ids = [p.product_tmpl_id.id + for p in product_product.browse( + cr, user, product_ids, context=context)] + uniq_ids = OrderedDict.fromkeys(template_ids).keys() + return self.name_get(cr, user, uniq_ids, context=context) class product_product(osv.osv): _name = "product.product"