Learn – Overriding name_get method in Odoo 8 with Example

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 display_name field.

Returns: list of pairs (id, text_repr) for each records
Return Type: list(tuple)

 

 

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.

  1. 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.

    name_get Override odoo 8
    Click on the picture to enlarge
  2. Now I will override the name_get method of res.country model.

    Code to Override name_get method in Odoo 8

    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.

  3. 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,
    name_get_3 Override odoo 8
    Click on the picture to enlarge

     

Here is a snapshot from Eclipse IDE:-

name_get_2 Override odoo 8
Click on the picture to enlarge

 

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,

Thank you.

Myself, Burhan Vakharia, am an Odoo developer, CS Lover living in Gandhinagar, Gujarat, India. I have a degree in Computer Science and Engineering from Babaria Institute of Technology. I am currently working as Odoo Developer. Apart from Odoo Framework, I have worked on C# .Net and Android Platforms. You can contact me via my social profiles shown below.

Post Author: Burhan Vakharia

Myself, Burhan Vakharia, am an Odoo developer, CS Lover living in Gandhinagar, Gujarat, India. I have a degree in Computer Science and Engineering from Babaria Institute of Technology. I am currently working as Odoo Developer. Apart from Odoo Framework, I have worked on C# .Net and Android Platforms. You can contact me via my social profiles shown below.

12 thoughts on “Learn – Overriding name_get method in Odoo 8 with Example

    Jesse

    (January 22, 2016 - 3:58 pm)

    Nice Example.

    Rose

    (January 22, 2016 - 5:09 pm)

    Implemented the code and it works perfectly as I wanted. Nice Blog.

    Donte

    (January 23, 2016 - 12:29 am)

    It’s nearly impossible to find well-informed people for this topic, but you sound like you know what you’re
    talking about! Thanks

    Natalie

    (January 23, 2016 - 1:35 pm)

    It’s very effortless to find out any topic on net as compared to books,
    as I found this paragraph at this site.

    Larry

    (January 23, 2016 - 2:06 pm)

    I’m really inspired with your writing abilities. Anyway stay up the nice
    high quality writing, it’s uncommon to look a great weblog like this
    one these days..

    Burton

    (January 23, 2016 - 2:32 pm)

    If you would like to increase your odoo know-how simply
    keep visiting this web page and be updated with
    the most recent information posted here.

    Pankaj Goyani

    (February 12, 2016 - 10:13 pm)

    Given a good example……

    avan

    (March 9, 2016 - 7:36 pm)

    Awesome post. Thank You.

    Ashton

    (March 23, 2016 - 1:46 am)

    Very well explained. Thank you sir.

    Juan

    (June 21, 2018 - 9:49 pm)

    Awesome post! thank you so much. Suscribed to the newsletter.

      Burhan Vakharia

      (July 14, 2018 - 11:00 am)

      I am glad that it helped you. Keep it up. Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *