Skip to content

Commit

Permalink
[FIX] product.template: name_search compatibility with product.product
Browse files Browse the repository at this point in the history
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
  • Loading branch information
odony committed Feb 26, 2015
1 parent e700805 commit bf31ab6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions addons/product/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -797,9 +799,16 @@ def name_get(self, cr, user, ids, context=None):
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"
Expand Down

0 comments on commit bf31ab6

Please sign in to comment.