Skip to content Skip to sidebar Skip to footer

Odoo - Display Name Of Many2one Field Combination Of 2 Fields

In my module i have the following many2one field: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance') where xx.insurance.type is the following: class Insu

Solution 1:

If you don't want to alter the display name of the rest of the many2one related to the model xx.insurance.type, you can add a context in the XML view to the many2one whose display name you want to modify:

<fieldname="xx_insurance_type"context="{'special_display_name': True}"/>

And then, in your name_get function:

defname_get(self, cr, uid, ids, context=None):
    if context isNone:
        context = {}
    ifisinstance(ids, (int, long)):
        ids = [ids]
    res = []
    if context.get('special_display_name', False):
        for record in self.browse(cr, uid, ids, context=context):
            name = record.name
            percentage = record.insurance_percentage
            res.append(record.id, name + " - " + percentage + "%")
    else:
        # Do a for and set here the standard display name, for example if the standard display name were name, you should do the next forfor record in self.browse(cr, uid, ids, context=context):
            res.append(record.id, record.name)
    return res

Solution 2:

@api.depends('name', 'insurance_percentage')
    def name_get(self):
        res = []
        forrecordinself:
            name = record.name
            ifrecord.insurance_percentage:
                name = '[' + record.insurance_percentage+ ']' + name
            res.append((record.id, name))
        return res

Post a Comment for "Odoo - Display Name Of Many2one Field Combination Of 2 Fields"