Understanding Odoo Model Attributes: Keys to Effective Data Management.

Hello Odoo enthusiasts! Today, let's explore a crucial but often overlooked aspect of Odoo: model attributes. Unlike model fields, which store data, model attributes are special parameters that define the structure and behavior of models in Odoo.

1. What is a model attribute?

An attribute of a model in Odoo is a kind of built-in directive that informs Odoo's Object-Relational Mapping (ORM) about how to handle a specific model. These attributes influence everything, from how models are named and inherited to how data is ordered and validated.

2. Essential Model Attributes in Odoo:
  • _name: The unique identifier of the model. It is crucial for internal referencing within Odoo.​
  • _description: A textual description of the model, useful for documentation and clarity.​
  • _inherit: Allows inheritance of existing models, adding or overriding functionality.​
  • _order: Sets the default sorting order for records.​
  • _rec_name: Specifies the primary field for displaying record names.​

Examples of the first 5 attributes:

class ResPartner(models.Model):
_name = "res.partner"
_description = "Contact"
_inherit = "res.partner.parent"
_order = "sequence asc"
_rec_name = "name"
- res.partner is the technical name of the model. 
- The description given to the res.partner model is "Contact," which is similar to the model's name but not from a technical perspective.
- The model inherits from another model, "res.partner.parent," allowing it to access its fields and methods.
- The display order of records will be in ascending order based on the "sequence" field.
- Lastly, the value displayed during a search on this model will be the value present in the "display_name" field.
  • _table: Allows you to define a specific table name in the database.​
  • _sql_constraints: Database-level constraints to ensure data integrity.​
  • _inherits: For SQL inheritance, where fields from the parent model are integrated into the child model.​
class ResPartner(models.Model):
_inherit = "res.partner"
_sql_constraints = [('unique_ref', 'UNIQUE(ref)', 'Ref must be unique!')]

ref = fields.Char(string="Ref")
The example below demonstrates the use of the _sql_constraints attribute to define the uniqueness of a field through a SQL constraint.


3. Conclusion: The Utility of Model Attributes

Understanding model attributes in Odoo is essential for any developer working with this platform. They play a key role in defining the structure and behavior of models, which directly impacts how applications are built and function. Mastering these attributes can lead to better module design and more efficient utilization of Odoo's features.

If you enjoyed this article, feel free to read the next one, which discusses field attributes, equally important in Odoo development!

Understanding Odoo Model Attributes: Keys to Effective Data Management.
Exaly, Renaud De Boeck December 29, 2023
Share this post
Sign in to leave a comment
Understanding Field Attributes in Odoo: An Essential Guide