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 default_get method in Odoo 8 with Example – Odoo Technical
Overriding default_get method

Greetings Everyone, in this blog-post you will learn how to override default_get method in Odoo 8.

The syntax of default_get is:

default_get(fields) → default_values

Return default values for the fields in fields_list. Default values are determined by the context, user defaults, and the model itself.

Parameters: fields_list – a list of field names
Returns: a dictionary mapping each field name to its corresponding default value; the keys of the dictionary are the fields in fields_list that have a default value different from False.

 

When is the default_get method called?

The default_get method of a model is called whenever a new record for that particular model is created.

Example of default_get method in Odoo 8:-

Here, what I want to do is, I want to pass default values for country, city, website, and email fields in res.partner model.

  1. This was the scenario in the beginning. Whenever i clicked the ‘Create’ button in Customers Form (ie. res.partner model), it opened up a blank form view as shown in the picture below.
    default_get image of res.patner
    Click on the picture to maximize

     

    Here as you can see all the fields in the form are blank by default. Now I want to pass the default values for country, city, website, and email fields.

  2. Now I will override the default_get method of res.partner model.

    Code to Override default_get method in Odoo 8

    from openerp import models,api
    
    class res_partner(models.Model):
        _inherit = 'res.partner'
    
        @api.model
        def default_get(self, vals):
            res = super(res_partner, self).default_get(vals)
            country_ids = self.env['res.country'].search([('code','=','IN')])
    
            if country_ids:
                res.update({
                            'country_id':country_ids[0].id, # Many2one field
                            'city': 'Gandhinagar',
                            'website': 'www.odootechnical.com',
                            'email' : 'contact@odootechnical.com'
                           })
            return res
    

    Here, first call the super method of res_partner method. It will return you a dictionary with keys as the field name and default values of fields if given.

    I searched for the Country India in res.country model and passed its id as value to the Many2one country_id field. ie. I updated the dictionary returned by the super method.

    @api.model because the record is not yet created and hence it does not have a id.

    Similarly, updated the fields for city, email and website and returned the dictionary.

  3. Now after restarting the server when I click on the ‘Create’ on Customers, those fields are automatically filled as shown in the picture below,

    default_get 3 image of res.patner
    Click on the picture to maximize

Here is a snapshot from Eclipse IDE:-

default_get method python code of res.patner
Click on the picture to maximize

Hope you got an insight and understanding of the default_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
2 thoughts on “Learn – Overriding default_get method in Odoo 8 with Example”


Warning: Attempt to read property "comment_ID" on null in /home/odootech/domains/odootechnical.com/public_html/wp-includes/comment-template.php on line 677

Warning: Attempt to read property "user_id" on null in /home/odootech/domains/odootechnical.com/public_html/wp-includes/comment-template.php on line 28

Warning: Attempt to read property "comment_ID" on null in /home/odootech/domains/odootechnical.com/public_html/wp-includes/comment-template.php on line 48
Leave a Reply to Anonymous Cancel reply

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