Odoo's ORM (Object-Relational Mapping) framework is central to its development ecosystem. At the heart of the ORM are model attributes, which define the structure, behavior, and interaction of models with the database. Understanding these attributes is crucial for creating robust and efficient applications. This guide delves into Odoo 18 model attributes, explaining their purpose and best use cases.
What Are Model Attributes?
Model attributes are configurations applied to Odoo models to define their properties and behavior. They control database interactions, specify inheritance, and handle data constraints. Developers can customize these attributes to meet specific requirements, making Odoo a flexible and scalable solution.
Core Model Attributes in Odoo
1. _auto – Automating Table Creation
The _auto attribute controls whether Odoo automatically creates a database table for the model.
Default Values:
True for standard and transient models.
False for abstract models.
Usage:
If _auto is False, developers need to override the init() method to manually create the database table.
2. _log_access – Access Logging
This attribute determines whether Odoo generates and updates access logs for the model.
Default Value: Inherits the value of _auto.
Purpose: Useful for tracking user actions and changes to records.
3. _table – Custom SQL Table Name
Purpose: Specifies the SQL table name for the model.
Default: Automatically derived from the _name attribute if _auto is True.
4. _sequence – SQL Sequence
Purpose: Defines the SQL sequence used for generating unique IDs in the model.
Benefit: Ensures the uniqueness of key fields like id.
5. _sql_constraints – Enforcing Data Integrity
Purpose: Allows developers to define SQL constraints for the model.
Structure: A list of tuples in the format (name, sql_definition, error_message).
Example:
_sql_constraints = [
('unique_name', 'UNIQUE(name)', 'The name must be unique.')
]
6. _register – Registry Visibility
Default Value: False.
Purpose: Controls whether the model is visible in Odoo’s registry.
7. _abstract – Abstract Models
Default Value: True for abstract models.
Purpose: Marks the model as abstract, meaning it serves as a base for other models and does not create a database table.
8. _transient – Transient Models
Default Value: True.
Purpose: Identifies a model as transient, typically used for temporary data.
9. _name – Defining the Model Name
Purpose: Specifies the technical name of the model.
Example:
_name = 'my.custom.model'
10. _inherit – Model Inheritance
Purpose: Used to extend or modify an existing model.
Usage: Specify the name of the model to inherit.
Example:
_inherit = 'res.partner'
11. _description – Informal Name
Purpose: Provides a human-readable name for the model, useful for documentation and debugging.
Example
_description = 'Custom Partner Model'
12. _inherits – Delegation Inheritance
Purpose: Implements delegation inheritance by mapping parent models to foreign key fields.
Structure: {‘parent_model’: ‘m2o_field’}.
Example:
_inherits = {'parent.model': 'parent_id'}
13. _rec_name – Record Labeling
Purpose: Specifies the field used for labeling records in the user interface.
Default: The name field.
Example:
_rec_name = 'custom_field'
14. _order – Default Sorting
Purpose: Specifies the default sorting order for records.
Default Value: 'id'.
Example:
_order = 'name asc'
15. _parent_name – Parent Field in Hierarchies
Purpose: Identifies the many2one field used as the parent in hierarchical data structures.
Default Value: 'parent_id'.
16. _parent_store – Computing Parent Path
Purpose: Computes the parent_path field for hierarchical models.
Default Value: False.
17. _date_name – Calendar View Field
Purpose: Defines the date field used in default calendar views.
Default Value: 'date'.
18. _fold_name – Folding Groups in Kanban Views
Purpose: Specifies the field used to determine which groups are folded in Kanban views.
Best Practices for Using Model Attributes
Choose the Right Model Type:
Use _abstract or _transient models only when necessary. Abstract models are great for reusability, while transient models work well for temporary data.
Define Constraints Early:
Leverage _sql_constraints to enforce data integrity at the database level. This reduces the chances of erroneous data being saved.
Use Descriptive Names:
Provide meaningful values for _name, _description, and _rec_name to improve readability and maintainability.
Leverage Inheritance Wisely:
Use _inherit and _inherits effectively to extend models or implement delegation inheritance without duplicating functionality.
Optimize Queries:
Use _order and _parent_store to ensure efficient sorting and hierarchy management for large datasets.
Model attributes are a cornerstone of Odoo development, providing developers with the tools needed to define and manage data models effectively. Attributes like _auto, _inherit, and _sql_constraints offer flexibility while maintaining structure and data integrity. By understanding and applying these attributes, developers can build scalable and efficient applications tailored to business requirements. Mastering these attributes is essential for any developer looking to excel in Odoo 18 development.
To read more about Model Attributes in Odoo 17, refer to our blog Model Attributes in Odoo 17.