Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 199

Deprecated: Return type of WPCF7_FormTag::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 396

Deprecated: Return type of WPCF7_FormTag::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 388

Deprecated: Return type of WPCF7_FormTag::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 382

Deprecated: Return type of WPCF7_FormTag::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 400

Deprecated: Return type of WPCF7_Validation::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 78

Deprecated: Return type of WPCF7_Validation::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 72

Deprecated: Return type of WPCF7_Validation::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 59

Deprecated: Return type of WPCF7_Validation::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 82

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/script-loader.php on line 348

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp.php on line 173
Learn – Overriding name_get method in Odoo 8 with Example – Odoo Technical
Overriding name_get method odoo 8

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

    from openerp import models,api
    
    class res_country(models.Model):
        _inherit = 'res.country'
    
        @api.multi
        def 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.

  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.

By Burhan Vakharia

Having 6+ years of experience working on Odoo with projects covering several business verticles like Retail industry, Manufacturing, Service, Rental, Education, Medical and many more. I am having experience working on Odoo projects from several countries across the globe.


Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/wp-db.php on line 3030
10 thoughts on “Learn – Overriding name_get method in Odoo 8 with Example”
  1. 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..

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

Leave a Reply

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