Reputation: 1134
We need to modify the product search behavior in the "New" quotation page. We need to make it faster, because we have a lot of products (more that 10 millions)
In the "Quotations" page, when the user creates a new quotation and adds a new order line and starts typing in the "Product field", Odoo starts searching automatically while the user is typing. Odoo searches in all the products and takes very long time to respond while the user is typing in the "Product" field.
I have overridden the product_id_change
method of the sale.sale_order
class and changed it. However, it did not work!
In order to make the product lookup faster, I want Odoo to get only the product(s) that have default_code
equal to the value entered in the product field. There is no need to search in other fields such as the product name. How can I achieve this?
Upvotes: 2
Views: 6505
Reputation: 11
@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()
Upvotes: 1
Reputation: 9620
You should override the name_search
method. Check some example in the source code. This is the name_search
method in the product.template
model. You should remove or replace the name
field in that code:
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)
You may want to do the same in the product.product
model.
I hope this helps you
Upvotes: 2