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

many2one

By
aci aisha
on 2/7/15, 6:35 AM 664 views

Friends,

In usual odoo selects the name field in many2one.

But how can i get other fields in many2one..(without using related)..

Eg:-I want to select the products in a new view of my custom module..

Thanks

 

please provide example or describe more.

Emipro Technologies Pvt. Ltd.
on 2/7/15, 9:26 AM
3

Openies Services

--Openies Services--
925
| 2 2 4
Vadodara, India
--Openies Services--

We provide Odoo Services.

Contact us on : contact@openies.com

Openies Services
On 2/8/15, 2:23 AM

Hello,

If you wanted to extend the search on the many2one field then you need to override the name_search method like 

def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
    return super(class_name, self).name_search(cr, uid, name, args=args, operator=operator, context=context, limit=limit)

It will return List of tuple, which is returned by name_get method.

In odoo name_get method is responsible to get name on the many2one field like [p123] Product 123

def name_get(self, cr, uid, ids, context=None):
    rerturn return super(class_name, self).name_get(cr, uid, ids, context=context)

Hope this helps in understanding name_search & name_get method in odoo.

 

0
Mohamed Essakali
On 2/7/15, 11:18 AM

You can change name_get and concatenate other fields. You can see example in addons.

For example if want to display field1 concatenated to field2 as "field1 / field2" in your many2one list use this code:

    def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['field1', 'field2'], context)
        res = []
        for record in reads:
            field1 = record['field1']
            field2 = record['field2']
            if field2:
                name = field2 + ' / ' + field1
            else:
                name =  field1
            res.append((record['id'], name))
        return res

    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
        if not args:
            args = []
        if not context:
            context = {}
        if name:
            # Be sure name_search is symetric to name_get
            ids=[]
            name_splited = name.split(' / ')
            if len(name_splited) > 1:
                field1 = name_splited[1]
                field2 = name_splited[0]
                ids += self.search(cr, uid, [('field1', operator, field1),('field2', operator, field2)] + args, limit=limit, context=context)
            else:
                ids += self.search(cr, uid, ['|',('field1', operator, name),('field2', operator, name)] + args, limit=limit, context=context)
        else:
            ids = self.search(cr, uid, args, limit=limit, context=context)
        return self.name_get(cr, uid, ids, context)

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: 2/7/15, 6:35 AM
Seen: 664 times
Last updated: 3/30/15, 10:57 AM