When creating a new model in Odoo or extending an existing one, you
can define various attributes to control the model’s behavior, data
structure, and how it interacts with the Odoo framework.
These attributes, often set as class variables, provide powerful
tools for customizing how your model is stored in the database, how
it behaves in the ORM (Object-Relational Mapping), and how it
integrates with other modules.
Here’s a rundown of the most commonly used model attributes in Odoo
18:
Core Model Attributes
- _name: The technical name of the model (e.g.,
'res.partner'). Required for all models.
- _description: A human-readable name for the
model, shown in the UI (e.g., "Customer").
- _inherit: Used to inherit an existing model and
extend its fields or functionality.
- _inherits: Allows model inheritance by
composition. It’s a dictionary mapping parent models to their
related many2one fields.
- _rec_name: The field used as the display name
for records. Defaults to 'name' if not set.
- _order: Defines the default sorting order for
records (e.g., _order = 'create_date desc').
Technical & Database Behavior
- _auto: True/False. If True (default), Odoo
automatically creates a database table for the model. If False,
you must manually define the table, typically used for abstract
models.
- _table: Sets a custom table name for the model
if _auto is True.
- _sequence: Specifies a SQL sequence for
generating IDs. If None, Odoo auto-generates one.
- _sql_constraints: A list of custom SQL
constraints for data integrity. Format: [('constraint_name',
'SQL expression', 'Error message')]
- _register: True/False. Determines whether the
model is registered in the Odoo model registry. Useful for
creating base models or internal tools.
Model Types
- _abstract: True. Marks the model as abstract —
no database table is created, and it’s only used for
inheritance.
- _transient: True. Declares a TransientModel,
used for temporary data (e.g., wizards). Records in these models
are automatically deleted after a certain time.
Security & Access Logging
- _log_access: True/False. Controls automatic
creation and update of metadata fields like create_uid,
create_date, write_uid, and write_date.
- _check_company_auto: True/False. Ensures
company consistency across related records when working in
multi-company setups. If enabled, _check_company is triggered
automatically during create and write.
Hierarchical & UI Behavior
- _parent_name: 'parent_id'. Defines the field
used to build a hierarchical structure (e.g., parent-child
categories).
- _parent_store: True/False. Enables the
computation of the parent_path field, useful for efficient tree
and hierarchical queries.
- _date_name: 'date'. Specifies which date field
is used as the default in calendar views.
- _fold_name: 'fold'. Indicates the field used to
define which kanban groups should appear folded by default.