JordanBelf
JordanBelf

Reputation: 3348

How to use a many2one field to display two fields together in OpenERP (odoo)?

I have a column where I currently have a many2one field which brings the Name of an employee from another class. The thing is that I need more than just the name, I would like to bring Name + last name.

I understand I have to use a function field but I don't understand how to tell the new function where to look for the values.

If anyone can point me documentation or a similar example I can follow I will really appreciate it.

Thanks!

Upvotes: 1

Views: 5523

Answers (1)

Hardik Patadia
Hardik Patadia

Reputation: 1999

You can use 'name_get' method to achieve this.

def name_get(self,cr,uid,ids,context=None):
    result = {}
    for record in self.browse(cr,uid,ids,context=context):
        result[record.id] = record.name + " " + record.lastname

    return result.items()

Following is just an example:

There is a model 'sale.order', in that there is a model 'res.partner' as many2one. So let's say you want to display 'First Name' and 'Last Name' of that partner in that many2one field.

So for that you have to write your code in the model 'res.partner', not in the 'sale.order' model.

Here is the code in 'res.partner' model.

class res_partner_extention(osv.osv):

_inherit = 'res.partner'

def name_get(self,cr,uid,ids,context=None):
    result = {}
    for partner in self.browse(cr,uid,ids,context=context):
        result[partner.id] = partner.name + " " + partner.lastname

    return result.items()

So now, in the 'sale.order' when you will try to look into the partner field, when you click on that many2one partner field it will show you 'First Name' as well 'Last Name'. So you don't need to anything 'sale.order' model nor you have to define any field as 'field.function'.

Upvotes: 1

Related Questions