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.
- 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.
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.
- 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.
- Now after restarting the server when I click on the ‘Create’ on Customers, those fields are automatically filled as shown in the picture below,
Here is a snapshot from Eclipse IDE:-

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.
Explained very well. Great Blog. Must Follow.
I cannot thank you enough for the forum post.Much thanks again. Great. Toy