KEMBAR78
How to Setup Default Value for a Field in Odoo 17 | PPTX
How to Setup Default Value
for a Field in Odoo 17
Enterprise
Introduction
Enterprise
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

How to Setup Default Value for a Field in Odoo 17

  • 1.
    How to SetupDefault Value for a Field in Odoo 17 Enterprise
  • 2.
    Introduction Enterprise In Odoo, wecan 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
  • 3.
    Enterprise Passing Values ThroughKwargs ● 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.
  • 4.
    Enterprise 1. Set ValuesDirectly ● 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')
  • 5.
  • 6.
    Enterprise 2. Set Valuesusing 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,
  • 7.
  • 8.
    Enterprise 3. Execute afunction 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)
  • 9.
    Enterprise ● In theexecute 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.
  • 10.
  • 11.
    Enterprise Setting value withdefault_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.
  • 12.
    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
  • 13.
    Enterprise ● In thisexample, 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.
  • 14.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com