Jan
21
2016

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.

Share This!
Share On Twitter
Share On Linkdin
Contact us

Related Posts

About the Author: Burhan Vakharia

I, Burhan Vakharia, am a python 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 at Serpent Consulting Services Pvt. Ltd., Gandhinagar. Apart form Odoo Framework, i have worked on C# .Net and Android Platforms.

2 Comments+ Add Comment

  • Explained very well. Great Blog. Must Follow.

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

Leave a comment

Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Contact us
Hide Buttons