Greetings Everyone, in this blog-post you will learn how to override name_getÂ method in Odoo 8.
The syntax of name_getÂ is:
name_get(self) —> [(id, name), …]
Returns a textual representation for the records in
self. By default this is the value of the
|Returns:||list of pairs
Example of name_get method in Odoo 8:-
Here, what I want to do is, I want to view country and the country code both as name for any country.
- This was the scenario in the beginning. Whenever I clicked Many2one field of country (which calls name_get method) it only showed the country name as display value as shown in the picture below. My goal isÂ to see the country name + country code as display.
- Now I will override the name_get method of res.country model.
Code to Override name_get method in Odoo 8 –name_get method overridden Odoo 8Python123456789101112131415161718from openerp import models,apiclass res_country(models.Model):_inherit = 'email@example.com name_get(self):res = super(res_country, self).name_get()data = for country in self:display_value = ''display_value += country.name or ""display_value += ' ['display_value += country.code or ""display_value += ']'data.append((country.id, display_value))return data
Here, I need to prepare a list of tuple wich is passed as return value to the method.
Inside the loop of self, I need to prepare a string which I want as the display value. Here i concatenated the name and code. i.e display_value in above code
Then prepare a tuple. The first value in the tuple is the id of the record and the second value is the string (display) that we prepared above. Append all the tuples in a list and return.
- Now after restarting the server when I click on the Many2one field of country, country name + code is seen in the view as shown in the picture below,
Here is a snapshot from Eclipse IDE:-
Hope you got an insight and understanding of the name_getÂ method in Odoo 8 and the way of overriding it.
Please Comment below if you have any doubts,