Overriding default_get methodOverriding default_get method Odoo 8

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.

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

Leave a Reply

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