
Company-Dependent Fields in Odoo 18
Introduction
In Odoo, managing data across multiple companies can be challenging, especially when specific fields need to vary between companies. To simplify this, Odoo provides company-dependent fields, allowing certain field values to be tied to a specific company. This blog will guide you through the concepts, examples, and usage of company-dependent fields in Odoo 18.
What Are Company-Dependent Fields?
A company-dependent field ensures that a field’s value is stored uniquely for each company. For example, a product’s sales unit of measure (UoM) or a specific cost can vary between companies.
To define a company-dependent field in Odoo, you use the company_dependent attribute in the field definition. Let’s look at an example.
Defining a Company-Dependent Field
Here’s how you can define a company-dependent field:
company_dependent=True: Specifies that the sale_uom_id field will store unique values for each company.
Reading Values from Company-Dependent Fields
When working with company-dependent fields, it’s essential to ensure you’re retrieving the value for the correct company. Odoo provides the with_company method to handle this seamlessly.
Here’s an example of how to fetch a company-dependent field value:
with_company(record.company_id): Ensures the standard_price is fetched for the active company (record.company_id).
When to Use Company-Dependent Fields
Varying Data Between Companies: For fields like pricing, sales UoM, or costs that differ across companies.
Centralized Configuration: To maintain shared records while allowing certain field values to be company-specific.
Simplified Multi-Company Logic: Reduces complexity by leveraging Odoo’s built-in mechanisms for handling company contexts.
Conclusion
Company-dependent fields in Odoo 18 provide a powerful way to handle company-specific data while leveraging Odoo’s multi-company features. By using company_dependent=True and the with_company method, you can manage such fields effectively, ensuring your application maintains both flexibility and accuracy. Whether you’re working with sales UoMs, costs, or other data, this feature simplifies operations across companies while preserving the integrity of your data.