How to Setup Default Value for a Field in Odoo 17

CelineGeorge1 1,359 views 14 slides Jun 19, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

In Odoo, we can set a default value for a field during the creation of a record for a model. We have many methods in odoo for setting a default value to the field.


Slide Content

How to Setup Default Value for a Field in Odoo 17 Enterprise

Introduction Enterpr ise In Odoo, we can set a default value for a field during the creation of a record for a model. We have many methods in odoo for setting a default value to the field. 1. Passing Values Through Kwargs 2. Setting value with default_get function

Enterprise Passing Values Through Kwargs A field can have numerous kwargs specified, such as necessary, read-only, and many more. The Kwargs are special symbols used to pass arguments, known as keyword arguments. kwarg is the default, which aids in setting a field's default value at the moment a model is created.

Enterprise 1. Set Values Directly In this, 'state' field is a selection field and the default value of the 'state' field is set as draft i.e, Draft. state = fields.Selection([('draft', 'Draft'), ('posted', 'Posted'),('cancel', 'Cancelled')], default='draft')

Enterprise

Enterprise 2. Set Values using default lambda function The syntax of the lambda function: lambda arguments: expression user_id = fields.Many2one( 'res.users', string='Salesperson', default=lambda self:self.env.user,

Enterprise

Enterprise 3. Execute a function and return values def _default_employee(self): return self.env.user.employee_id employee_id = fields.Many2one('hr.employee', string="Employee",default=_default_employee, required=True, ondelete='cascade', index=True)

Enterprise In the execute a function and return values method, we can set the default value of the field by executing a function. define a function _default_employee() and then define a field employee_id and set default=_default_employee.

Enterprise

Enterprise Setting value with default_get function To setting default values for custom fields using the default_get function in Odoo, you can define a method named default_get within your model class. This method should return a dictionary where the keys are the field names and the values are the default values you want to set.

Enterprise class SaleOrder(models.Model): _inherit = 'sale.order' sales_person_contact = fields.Many2one('res.partner', string="Salesperson Contact") sales_person_email = fields.Char(string="Salesperson Email") @api.model def default_get(self, fields): res = super(SaleOrder, self).default_get(fields) res.update({ 'sales_person_contact': self.env.user.partner_id.id or False, 'sales_person_email' : self.env.user.partner_id.email or False }) return res

Enterprise In this example, when a new record of SaleOrder is created, the default_get function will be called automatically, and it will return a dictionary containing default values for the specified fields (sales_person_contact and sales_person_email). These default values will be populated in the form when creating a new record unless overridden by the user. You can adjust the default values as per your requirements in the default_get method. This method allows you to dynamically set default values based on certain conditions if needed.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com