Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

ORM API - method name_search. When should I use it and redefine it?

By
Eduardo BP
on 5/11/15, 11:15 AM 3,635 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.
0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 5/12/15, 2:03 AM

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)



Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/11/15, 11:15 AM
Seen: 3635 times
Last updated: 5/12/15, 2:03 AM