Learn – Overriding default_get method in Odoo 8 with Example

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

    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.

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.

4 thoughts on “Learn – Overriding default_get method in Odoo 8 with Example

    Rosalyn

    (February 23, 2016 - 11:45 am)

    Explained very well. Great Blog. Must Follow.

    Williamlisp

    (May 22, 2016 - 12:29 pm)

    I cannot thank you enough for the forum post.Much thanks again. Great. Toy

    Shobha

    (February 14, 2018 - 10:54 pm)

    Well explained

Leave a Reply

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