Enable Dark Mode!
a-complete-guide-to-default-values-in-odoo-19.jpg
By: Safa KB

A Complete Guide to Default Values in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

Default values in Odoo 19 improve the user experience by pre-filling fields with initial data, reducing repetition, and maintaining consistency across records. In this blog, we will explore different ways to set default values in Odoo 19.

1. Using Defaults in Fields

The most straightforward approach to setting a default value is in the field definition itself using the default attribute.

from odoo import models, fields
class Employee(models.Model):
   _name = "company.employee"
   name = fields.Char(string="Employee Name")
   age = fields.Integer(string="Age", default=25)  # Default age set to 25
   active = fields.Boolean(string="Active", default=True)

Here, whenever a new employee record is created, the age field by default will be 25, and active will be True.

2. The default_get() Method

For more complex situations depending on multiple fields or conditions, you can customize the default_get() method.

class Employee(models.Model):
   _inherit = "company.employee"
   def default_get(self, fields_list):
       defaults = super().default_get(fields_list)
       defaults['name'] = "New Employee"
       defaults['age'] = 30
       return defaults

default_get() lets you calculate default values dynamically before the record is loaded.

3. Context-Driven Defaults

Odoo's context mechanism enables passing default values dynamically upon opening a form view. This helps you in situations such as defaulting a field according to the parent record.

# In an action definition
{
   'type': 'ir.actions.act_window',
   'res_model': 'company.employee',
   'view_mode': 'form',
   'context': {'default_age': 28, 'default_active': True},
}

When the form opens, this will automatically set the age field to 28.

4. Dynamic Defaults

Dynamic defaults are calculated values that depend upon conditions, computation, or other fields.

from odoo import api
class Employee(models.Model):
   _inherit = "company.employee"
   @api.model
   def _default_name(self):
       return f"Employee_{self.env['company.employee'].search_count([]) + 1}"
   name = fields.Char(default=_default_name)

Here, the default name is generated based on the total number of employee records.

5. Inherited Defaults

You can override or extend default values when you're inheriting models without having to change the original model.

class EmployeeExtended(models.Model):
   _inherit = "company.employee"
   age = fields.Integer(default=35)  # Overrides the previous default

Especially useful for the creation of custom modules that extend Odoo standard ones.

6. Default Values with User Groups

Odoo also allows defining default values with regard to user groups. This means that different users will see different default values, depending on the roles they fall into.

from odoo import api, models
class Employee(models.Model):
   _inherit = "company.employee"
   @api.model
   def default_get(self, fields_list):
       defaults = super().default_get(fields_list)
       if self.env.user.has_group('base.group_system'):
           defaults['age'] = 40
       else:
           defaults['age'] = 25
       return defaults

Here, system administrators will see a default age of 40, while other users see 25.

Conclusion

Default values are a powerful feature of Odoo 19 that will streamline data entry and improve efficiency.

Defaults can be set at multiple levels:

  1. Field-level defaults for static values.
  2. default_get() for computed or conditional defaults.
  3. Context-driven defaults for parent-child scenarios.
  4. Dynamic defaults based on computations or other records.
  5. Inherited defaults for extending existing modules.
  6. User group defaults are to customize the defaults according to roles.

By understanding and using these methods well, you can improve the usability of your Odoo 19 apps significantly.

To read more about What are the Key Field Parameters in Odoo 18, refer to our blog What are the Key Field Parameters in Odoo 18.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message