python38292
python38292

Reputation: 69

Put in _rec_name a combination of two fields in Odoo 11

Hi I have the following model:

# -*- coding: utf-8 -*-


from odoo import models, fields, api

class myclass(models.Model):
    _name = 'myproject.myclass'
    _rec_name = 'field1'

     field1= fields.Char('Field1', size=64, required=True)
     field2= fields.Char('Field2', size=64, required=True)
     field3= fields.Char('Field3', size=64, required=True)


def name_get(self, cr, uid, ids, context=None):
    res = []
    fields= self.browse(cr, uid, ids, context)
    for field in fields:
        res.append((field .id, field.field1+ ' ' + field.field2))
    return res

The problem is that Odoo only print the field in _rec_name, ie, 'field1'.

I test solutions in:

concatenate firstname and lastname and fill into name field in odoo

https://www.odoo.com/es_ES/forum/ayuda-1/question/how-to-display-custom-value-in-many2one-field-in-odoo-11-144209

https://gist.github.com/vijoin/b370e68a06d89af5b354

Upvotes: 1

Views: 1600

Answers (1)

CZoellner
CZoellner

Reputation: 14801

You should stick to the new API and also try to stick to some coding guidelines. Two very obvious things are the class name and the variable name field which is a business object record and not a field.

class MyClass(models.Model):
    _name = 'myproject.myclass'
    _rec_name = 'field1'

     field1 = fields.Char('Field1', size=64, required=True)
     field2 = fields.Char('Field2', size=64, required=True)
     field3 = fields.Char('Field3', size=64, required=True)

    @api.multi
    def name_get(self):
        res = []
        for record in self:
            res.append((record.id, "%s %s" % (record.field1, record.field2)))
        return res

Upvotes: 1

Related Questions