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.

1

how to give rec_name as a combination of two fields

By
Soohoo
on 12/25/13, 9:52 AM 3,205 views
_rec_name = 'some_field_1' + 'some_field_2'

i want to use combination of two field as record name

2

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 12/28/13, 12:58 PM

many2one field by default displays either name field or the field which is defined in _rec_name. If you want to display value of other fields then you need to override name_get method in your object.

For example I want to display two fields (name, name2) in my many2one field:

def name_get(self, cr, uid, ids, context=None):
    if not len(ids):
        return []
    res = [(r['id'], r['name'] and '%s [%s]' % (r['name'], r['name2']) or r['name'] ) for r in self.read(cr, uid, ids, ['name', 'name2'], context=context) ]
    return res

You may refer: ORM Method: name_get.

1

Ray Carnes

--Ray Carnes--
15677
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

Ray Carnes
On 12/25/13, 6:42 PM

You need to override the name_get function for the class.

By default this function returns either the value of name or the value of the field specified in _rec_name.

Product Category gets composite values this way.

image description

There is a function, overriding the default, at \addons\product\product.py:

def name_get(self, cr, uid, ids, context=None):
    if isinstance(ids, (list, tuple)) and not len(ids):
        return []
    if isinstance(ids, (long, int)):
        ids = [ids]
    reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
    res = []
    for record in reads:
        name = record['name']
        if record['parent_id']:
            name = record['parent_id'][1]+' / '+name
        res.append((record['id'], name))
    return res

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

0 follower(s)

Stats

Asked: 12/25/13, 9:52 AM
Seen: 3205 times
Last updated: 3/16/15, 8:10 AM