Master Advanced Model Attributes
Understanding Odoo Metadata
What Is Model Metadata?
First, model metadata refers to class-level attributes on an Odoo model. They define how Odoo stores, searches, and displays records. For example, setting _name tells Odoo the database table name. Moreover, flags like _log_access control audit logging. As a result, metadata drives core behavior before you declare any fields.
Why Use Odoo Metadata?
Secondly, applying Odoo metadata streamlines development. Furthermore, it ensures data integrity, enforces company rules, and optimizes queries. Therefore, you can reduce code complexity. In addition, proper metadata yields a consistent user interface. Consequently, teams maintain best practices across large projects.
Key Odoo Metadata Attributes
Below, you’ll explore the most vital metadata settings. You’ll learn how each attribute impacts your model.
_name & _description
Firstly, _name defines the internal model identifier and the database table. Meanwhile, _description gives a human-readable label in the UI.
class HrPayslip(models.Model):
_name = 'hr.payslip'
_description = 'Pay Slip'
- Transition: Moreover, choosing clear names promotes maintainability.
- Explanation: Odoo creates the table
hr_payslipbased on_name. - Tip: Always match
_nameto your module’s naming convention.
_rec_name & _rec_names_search
Secondly, _rec_name determines the default field for record labels in dropdowns. Meanwhile, _rec_names_search adds alternative fields for search.
class ProductCategory(models.Model):
_name = 'product.category'
_rec_name = 'complete_name'
_rec_names_search = ['name', 'complete_name']
- Transition: Furthermore, using both ensures flexible search.
- Effect: Odoo uses
complete_namein tree views and allows typingnamein search.
_order
Thirdly, _order sets the default record sorting for list and kanban views.
class ProductCategory(models.Model):
_order = 'complete_name asc'
- Transition: However, explicit
ascordescclarifies the direction. - Result: Records appear alphabetically by complete name.
_log_access
Next, _log_access toggles automatic logging of create_date, write_date, and user stamps.
class HrPayslip(models.Model):
_log_access = True
- Transition: Therefore, enabling this attribute captures audit trails.
- Use Case: Audit your payroll entries for compliance.
_auto
Then, _auto controls whether Odoo creates a backing database table. Setting it to False yields a “non-stored” model, often used for abstract or secret data.
class HrPayslip(models.Model):
_auto = False
- Transition: Consequently, you must create your own table or view manually.
- Warning: Use this sparingly to avoid missing tables.
_register
Moreover, _register dictates whether Odoo registers the model with ir.model. Setting _register = False hides internal models.
class HrPayslip(models.Model):
_register = False
- Transition: In addition, you prevent end users from seeing technical models.
- Example: Use on base mixin models.
_fold_name
Moreover, _fold_name defines the Boolean field that marks records as “folded” in tree views.
class HrPayslip(models.Model):
_fold_name = 'folded'
folded = fields.Boolean('Folded in kanban view')
- Transition: As a result, users collapse groups easily.
- Visual: Your kanban view respects
folded.
_check_company_auto
Furthermore, _check_company_auto enforces that related records share the same company. Odoo raises errors when mismatched.
class HrPayslip(models.Model):
_check_company_auto = True
- Transition: Therefore, you maintain multi-company data integrity.
- Benefit: You prevent accidental cross-company operations.
_parent_name & _parent_store
Next, for hierarchical models, _parent_name sets the Many2one field to the parent. Meanwhile, _parent_store enables efficient path queries.
class ProductCategory(models.Model):
_parent_name = 'parent_id'
_parent_store = True
parent_id = fields.Many2one('product.category', 'Parent Category')
- Transition: Consequently, Odoo builds nested category trees.
- Optimization:
parent_storestores the full path for fast searches.
Practical Tutorial: Implement Advanced Metadata in Odoo 18
Follow this tutorial to apply odoo metadata in a real module.
1. Setup Development Environment
Firstly, install Odoo 18 locally. Secondly, create a new custom addons path:
mkdir -p ~/odoo18/custom_addons/advanced_metadata
cd ~/odoo18/custom_addons/advanced_metadata
2. Create the Module Skeleton
Next, scaffold your module:
advanced_metadata/
├── __init__.py
├── __manifest__.py
├── models/
│ └── hr_payslip.py
│ └── product_category.py
└── views/
└── hr_payslip_views.xml
└── product_category_views.xml
- Transition: Then, initialize Python packages:
touch __init__.py models/__init__.py views/__init__.py
3. Define the Manifest (__manifest__.py)
In __manifest__.py, add metadata and assets:
{
'name': 'Advanced Metadata Demo',
'version': '1.0',
'category': 'Custom',
'depends': ['base', 'hr', 'product'],
'data': [
'views/hr_payslip_views.xml',
'views/product_category_views.xml',
],
'installable': True,
'application': False,
}
- Transition: Moreover, setting
application: Falsehides it from the Apps dashboard.
4. Add Model with Metadata
models/hr_payslip.py
from odoo import models, fields
class HrPayslip(models.Model):
_name = 'hr.payslip'
_description = 'Pay Slip'
_fold_name = 'folded'
_check_company_auto = True
_rec_names_search = ['name', 'struct_id']
_log_access = True
_register = False
_auto = False
name = fields.Char('Payslip Reference', required=True)
folded = fields.Boolean('Folded in Kanban View')
struct_id = fields.Many2one('hr.payroll.structure', 'Structure')
- Transition: Then, define the tree view extension:
views/hr_payslip_views.xml
<odoo>
<record id="hr_payslip_tree" model="ir.ui.view">
<field name="name">hr.payslip.tree</field>
<field name="model">hr.payslip</field>
<field name="arch" type="xml">
<tree string="Payslips" foldable="folded" editable="bottom">
<field name="name"/>
<field name="struct_id"/>
</tree>
</field>
</record>
</odoo>
- Explanation: Users see fold lines in the kanban and tree views.
models/product_category.py
from odoo import models, fields
class ProductCategory(models.Model):
_name = 'product.category'
_description = 'Product Category'
_parent_name = 'parent_id'
_parent_store = True
_rec_name = 'complete_name'
_order = 'complete_name'
name = fields.Char('Name', index='trigram', required=True)
parent_id = fields.Many2one('product.category', 'Parent Category', index=True, ondelete='cascade')
complete_name = fields.Char('Complete Name')
views/product_category_views.xml
<odoo>
<record id="product_category_tree" model="ir.ui.view">
<field name="name">product.category.tree</field>
<field name="model">product.category</field>
<field name="arch" type="xml">
<tree string="Categories">
<field name="complete_name"/>
</tree>
</field>
</record>
</odoo>
5. Install and Test Your Module
Firstly, restart your Odoo 18 server. Secondly, update the apps list in developer mode. Thirdly, install Advanced Metadata Demo. Consequently, you see:
- Payslip model with folding and logging features.
- Category model with hierarchical tree and optimized search.
Common Pitfalls & Best Practices
Avoid Overusing _auto = False
Firstly, setting _auto = False skips table creation. However, Odoo might raise “relation does not exist” errors if you forget to create the table manually. Therefore, use _auto = False only when you need a proxy or computed view.
Use _log_access Wisely
Secondly, turning on _log_access logs every create and write. Consequently, it can slow down operations on high-volume models. Therefore, enable it only for critical business data.
Leverage _rec_names_search
Moreover, adding _rec_names_search vastly improves search performance. In addition, it lets users find records by alternate fields. Therefore, always include common lookup fields.
Conclusion
In summary, mastering odoo metadata empowers you to control model behavior precisely. Furthermore, using attributes like _order, _parent_store, or _check_company_auto makes your application robust and scalable. Consequently, you deliver cleaner code and a better user experience. Therefore, apply these advanced metadata settings in your next Odoo 18 project to boost performance and maintainability.
source : https://www.odoo.com/documentation/18.0/
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.

