Search code examples
odoo

How to use name_get condition in odoo 12


class ResPartnerInherit(models.Model):
    _inherit = 'res.partner'


    is_specialist = fields.Boolean(string='Is Specialist')
    specialized_in = fields.Many2one('specialization',string='Specialization')
    hospital = fields.Char(string='Hospital')


    @api.depends('is_specialist')
    @api.multi
    def name_get(self):
       
        res = []
        self.browse(self.ids).read(['name', 'hospital'])
        for rec in self:
            res.append((rec.id, '%s - %s' % (rec.name, rec.hospital)))
        return res

What I'm trying to do here is, using name_get function When selecting a specialist his hospital needs to be shown, so I wanna give condition only for a specialist,there is a boolean field named is_specialist.so I wanna get the condition only when boolean is true


Solution

  • You just need to check is the partner is a specialist when building his name and if yes show also the hospital.

    @api.multi
    def name_get(self):
        res = []
        for rec in self:
            res.append((rec.id, '%s - %s' % (rec.name, rec.hospital) if rec.is_specialist else rec.name))
        return res