Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
5 Antwoorden
21730 Weergaven
_rec_name = 'some_field_1' + 'some_field_2'

i want to use combination of two field as record name

Avatar
Annuleer
Beste antwoord

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
Avatar
Annuleer
Beste antwoord

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.

Avatar
Annuleer
Beste antwoord

Hi Soohoo,

As the current answers are all very old and things have changed a bit I thought it would be good to add a new code sample:

def name_get(self):
return [(record.id, f"{record.some_field_one} - {record.some_field_two}) for record in self]

This way you can add as many fields/values as you want to the name_get and Odoo will combine them to show all the info you want.

Avatar
Annuleer

Thank you. Worked perfectly. You just forget " after {record.some_field_two}. Find below my implementation:
def name_get(self):
return [(record.id, f"{record.supplier_id.name} write any thing here {record.offer_price}") for record in self]

Beste antwoord

You can use a computed field:

class Test(models.Model):
_name = 'coding.test'
_rec_name = 'combination'

field1 = fields.Char(default='Bob')
field2 = fields.Char(default='Sponge')
combination = fields.Char(string='Combination', compute='_compute_fields_combination')

@api.depends('field1', 'field2')
def _compute_fields_combination(self):
for test in self:
test.combination = test.field1 + ' ' + test.field2
Avatar
Annuleer
Beste antwoord
I've tried in several ways,
but it always shows me this error

TypeError: name_get() takes at least 4 arguments (1 given)

Do I need something to include?


class komReon(models.Model):
_name = 'kom.reon'
_description = 'Sifarnik Reona'

reon_id = fields.Integer(string='Sifra reona')
name = fields.Char(string='Naziv reona')
parent_id = fields.Many2one('kom.reon')
parent_left = fields.Integer(string='Roditelj lijevo')
parent_right = fields.Integer(string='Roditelj desno')

    def name_get(self,cr,uid,ids,context={}):
    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

Avatar
Annuleer