Default Get Function in Odoo

Abid Patel
11-Oct-2024 Updated : 11-Oct-2024

Learn how to use the default_get function in Odoo to set default values for fields when creating new records. Step-by-step guide included.

Default Get Function in Odoo

The Default Get function in Odoo is a powerful feature that allows developers to set default values for fields when creating new records. This function can enhance user experience by pre-filling fields based on specific logic, reducing the need for users to enter repetitive data manually. In this blog post, we will explore how to implement the default_get function in Odoo with practical examples.

What is the Default Get Function?

The default_get function is a special method in Odoo that is automatically called when creating new records. It allows you to define default values for fields based on specific criteria or existing data. By overriding this method, you can customize the default values based on various conditions, such as user preferences, existing records, or other contextual data.

How to Override the Default Get Function

To use the default_get function, you need to inherit the model you want to customize. Here’s how to do it:

Step 1: Inherit the Model

Let’s say we want to set default values for the res.partner model, which manages contacts in Odoo. You first need to create a new class that inherits from the res.partner model.

Python

from odoo import models, fields

class CustomPartner(models.Model):
    _inherit = 'res.partner'

Step 2: Override the Default Get Method

Next, you can override the default_get method to define default values for specific fields:

Python

    def default_get(self, fields):
        res = super(CustomPartner, self).default_get(fields)
        
        # Set default values
        if 'customer' in fields:
            res['customer'] = True  # Set the default customer flag to True
        
        if 'supplier' in fields:
            res['supplier'] = False  # Set the default supplier flag to False
        
        return res

In this example:

  • ▹ We call the original default_get method using super() to ensure that we maintain any default values defined in the base model.
  • ▹ We then check if the customer and supplier fields are included in the fields list. If they are, we set their default values accordingly.

Using Context to Set Default Values

You can also use the context to set default values based on certain conditions. For example, if you want to set a default value for a field based on the current user’s preferences:

Python

    def default_get(self, fields):
        res = super(CustomPartner, self).default_get(fields)
        
        # Check the context for specific conditions
        if self.env.context.get('default_customer_type') == 'individual':
            res['customer'] = True
            res['supplier'] = False
        else:
            res['customer'] = False
            res['supplier'] = True
        
        return res

In this example:

  • ▹ We check the context for a key called default_customer_type. Based on its value, we set the customer and supplier fields accordingly.

Setting Default Values for Related Fields

The default_get method can also be used to set default values for related fields. For instance, if you want to set a default country for new partners:

Python

    def default_get(self, fields):
        res = super(CustomPartner, self).default_get(fields)
        
        # Set default country for new partners
        if 'country_id' in fields:
            default_country = self.env['res.country'].search([('code', '=', 'US')], limit=1)
            res['country_id'] = default_country.id if default_country else False
        
        return res

In this case:

  • ▹ We check if the country_id field is in the fields list and search for the country with the code 'US'.
  • ▹ If found, we set the country_id field with the corresponding country ID.

Best Practices for Using Default Get

When implementing the default_get method, consider the following best practices:

  • Always call the super method: Ensure that you maintain the base model's default values by calling super().
  • Keep logic simple: Avoid complex logic within default_get to maintain readability and performance.
  • Test thoroughly: Make sure to test your implementation to ensure that default values are set correctly under various conditions.

Conclusion

The default_get function in Odoo is a valuable tool for developers looking to enhance user experience by pre-filling fields with relevant data when creating new records. By following the steps outlined in this blog post, you can effectively implement and customize the default_get method to meet your specific business requirements.

For more Odoo development tips and tricks, be sure to check our website Free web snippets.

Make a Comment

Your email address will not be published. Required fields are marked *