Default_get method in odoo
The default_get method in Odoo is used to provide default values for fields when a new record is created.
How Does the default_get method Work?
The system invokes the default_get function to obtain the default values for the fields whenever a new record is generated in Odoo. The function accepts a list of field names as an input and outputs a dictionary with the field names as the keys and the default values for those fields as the values.
The basic Python code for the method default_get is
def default_get(self,fields_list):
res = super( class name, self).default_get(fields_list)
return res
where res is a dictionary with field name as its key values.
For Example, here we will be applying the default_get method in the sale order module
from odoo import api, models, fields
class SaleOrder(models.Model):
_inherit = 'sale.order'
@api.model
def default_get(self, fields_list):
res = super(SaleOrder, self).default_get(fields_list)
res['partner_id'] = 9
res['payment_term_id'] = 6
return res
As you can see I have mentioned res[“partner_id”] = 9 and res[“payment_term_id”] = 6 where ‘partner_id’ and ‘payment_term_id’ is the field names in the sale order form view. So when we create a new Sale order the fields related to ‘partner_id’ and ‘payment_term_id’ by default will get filled by with a record that has its corresponding id as 9 and 6
From the SQL table, we know that partner_id=9 corresponds to the name as Wood Corner
and the payment_term_id=6 corresponds to 2 months.
As mentioned before, creating a new sale order automatically updates the Customer field to Wood Corner and the Payment Terms field to 2 months
Conclusion
The default_get method is a Python function in the Odoo framework that allows developers to set default values for fields when creating new records. By customizing this method, you can enhance the user experience, ensure data consistency, and implement modern business logic.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote