support Click to see our new support page.
support for sales enquiry

How to change the display name of a record in Odoo

name of record in odoo

SajlaApril 22, 2024

Display name is the textual representation of a record. ie, the name being displayed in the relational fields defined based on that model and on top of the form view of a record.

Take a look at the following example. Here I’ve created a model for event types and another for event bookings with a many2one field to choose the event type.

The Event type model is defined as follows in the python file.

class EventType(models.Model):
_name = 'event.type'
_description = 'Event_type'
event_type = fields.Char(string='Event Name', required=True)
code = fields.Char(string='Event Code', required=True)

And the Event booking model as follows.

class EventBooking(models.Model):
_name = 'event.booking'
_description = 'Event Booking'
_rec_name = 'event_type_id'
partner_id = fields.Many2one('res.partner')
user_id = fields.Many2one('res.users', 'Responsible Person')
event_type_id = fields.Many2one('event.type')
booking_date = fields.Date()
date_from = fields.Datetime()
date_to = fields.Datetime()

I have created two records for event types like this.

And in the event booking form while we select the event type, the record name appears as follows.

In this blog, we will be discussing the different methods to change the display name of a record in Odoo.

1. ‘name’ field

If we have a ‘name’ field defined in the model, that field value will be displayed as the name of the record by default.

In the above example, I change the field ‘event_type’ to ‘name’ and upgrade the module to see the difference.

class EventType(models.Model):
_name = 'event.type'
_description = 'Event_type'
name = fields.Char(string='Event Name', required=True)
code = fields.Char(string='Event Code', required=True)

Now when we select the event type, the event type record is displayed as the
name field value.

 

2. _rec_name attribute

Suppose we need to display the ‘Code’ instead of the Name, we can do that by specifying the _rec_name attribute for the model.

class EventType(models.Model):
_name = 'event.type'
_description = 'Event_type'
_rec_name = 'code'
Upgrade the module to see the difference.

 

3. name_get() function (Up to V16) / _compute_display_name function (V17)

Suppose we need the display name to be a combination of multiple fields or to be in some other pattern, we can define the name_get() function in Odoo versions up to 16 or _compute_display_name() function in version 17 to achieve the same.

1.name_get() function

Here in the above example, we can define a name_get() function to get the display name in the format “Event Code - Event Name”.

def name_get(self):
res = []
for record in self:
name = '{} - {}'.format(record.code, record.name)
res.append((record.id, name))
return res


Note : We can add context based conditions in the function for displaying different names for the same record in different views.

2._compute_display_name() function0

 name_get() function has been deprecated and a new compute field “display_name” has been added in V17 for the textual representation of a record. And _compute_display_name is the compute function which assigns value to the display_name field. We can override the method to make the required changes in the record name. If needed, it can be made field-dependent using @api.depends and context-dependent using @api.depends_context decorators.

We can define the “_compute_display_name()” function to get the display name in the same format as follows.

@api.depends('code', 'name')
def _compute_display_name(self):
for rec in self:
if rec.code and rec.name:
rec.display_name = f"{rec.code} - {rec.name}"
else:
rec.display_name = f"{rec._name},{rec.id}"


In both the above cases, the display name will be changed into the specified format as depicted in the following image.

LinkedIn LinkedIn

Subscribe to our Newsletter

Sign up to receive more information about our latest offers & new product announcement and more.