Skip to Content
Menu
This question has been flagged
1 Reply
24292 Views

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.
Avatar
Discard

When should you use name_search method: https://goo.gl/7PHhPP

Best Answer

1. name_search and name_get are used when used as a related table is checked i.e. use of many2one
When a many2one field is created who have a relation to an object(res.partner), then how the value needs to be shown is done through name_get method and to search at many2one field uses name_search method.

2. Yes, it is due to change of API environment.

In v7,
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100)

In v8, @api.mode
def name_search(name='', args=None, operator='ilike', limit=100)



Avatar
Discard
Related Posts Replies Views Activity
2
Apr 25
2150
1
Feb 25
635
2
Feb 25
1101
2
Jan 25
1246
1
Dec 24
2179