Hi Odoo developers,
I have read the documentantion about the method 'name_search' of the Odoo 8 ORM API in the following link: https://www.odoo.com/documentation/8.0/reference/orm.html.
First question:
The problem is that I don't understand very well when and where the 'name_search' is useful. Could someone explain me with some example, please?.
Second question:
Also, I have seen modules created by the community that overwrite the 'name_search' method in a module. Why do they do this?.
I'm starting to develop with the Odoo 8 framework and there are difficult things to understand for me.
An example of the overwrited 'name_search' method is in the following link:
https://github.com/acsone/acsone-addons/blob/8.0/project_code/project_code.py
And here a part of the model source code of that example:
from openerp.osv import orm, fields
class project(orm.Model):
_inherit = "project.project"
def name_search(self, cr, uid, name, args=None, operator='ilike',
context=None, limit=100):
if not args:
args = []
args = args[:]
ids = []
if name:
ids = self.search(cr, uid,
[('code', '=like', name + "%")] + args,
limit=limit)
if not ids:
ids = self.search(cr, uid,
[('name', operator, name)] + args,
limit=limit)
else:
ids = self.search(cr, uid, args, context=context, limit=limit)
return self.name_get(cr, uid, ids, context=context)
def name_get(self, cr, uid, ids, context=None):
if not ids:
return []
if isinstance(ids, (int, long)):
ids = [ids]
reads = self.read(cr, uid, ids, ['name', 'code'], context=context)
res = []
for record in reads:
name = record['name']
if record['code']:
name = record['code'] + ' - ' + name
res.append((record['id'], name))
return res
In the above example the name_search method signature,
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100)
doesn't match the name_search method signature in the Odoo 8 ORM API:
name_search(name='', args=None, operator='ilike', limit=100)I guess it is because they are using the OpenERP v7 framework, but I'm not sure.
When should you use name_search method: https://goo.gl/7PHhPP