Odoo Model Meta Attributes with Examples
This document provides a detailed explanation of the most important meta attributes in Odoo models, along with practical examples where applicable.
1. _name
- Purpose: Technical identifier of the model.
- Example:
_name = 'library.book' - This defines a new model accessible via self.env['library.book'].
2. _inherit
- Purpose: Used to extend an existing model.
- Example:
_inherit = 'res.partner' - This lets you add fields or override methods in res.partner.
3. _inherits
- Purpose: Delegation inheritance using a foreign key to another model.
- Example:
_name = 'student.record'
_inherits = {'res.partner': 'partner_id'} - partner_id = fields.Many2one('res.partner', required=True)
- Fields of res.partner are accessible as if they belong to student.record.
4. _description
- Purpose: Human-readable name for the model.
- Example:
_description = 'Library Book Master' - Used in developer tools and admin UI.
5. _rec_name
- Purpose: Field shown in many2one dropdowns and views.
- Example:
_rec_name = 'isbn_code' - isbn_code = fields.Char('ISBN Code')
- Instead of name, isbn_code will display as label.
6. _order
- Purpose: Default sorting order for records.
- Example:
_order = 'published_date desc, name asc'
7. _sql_constraints
- Purpose: Define SQL constraints for uniqueness or checks.
- Example:
_sql_constraints = [('unique_isbn', 'UNIQUE(isbn_code)', 'ISBN must be unique!')]
8. _auto
- Purpose: Tells Odoo whether to create a table automatically.
- Default: True
- Example:
_auto = False # For SQL views
9. _table
- Purpose: Custom table name in the database.
- Example:
_table = 'lib_book_master'
10. _log_access
- Purpose: Automatically creates create_uid, create_date, etc.
- Example:
_log_access = False - Useful in models like logs or system jobs.
11. _check_company_auto
- Purpose: Enables automatic company consistency checks.
- Example:
_check_company_auto = True - Ensures all related records belong to the same company.
12. _register
- Purpose: Controls whether a model is registered.
- Default: True
- Example:
_register = False # Used in abstract models
13. _transient
- Purpose: Marks model as temporary (like wizards).
- Default: False
- Example:
_transient = True - Auto-deletes after a time period or on restart.
14. _constraints
- Purpose: Older method for Python-based validation (use @api.constrains instead).
- Example:
def _check_quantity(self):
for record in self:
if record.qty < 0:
return False
return True
_constraints = [(_check_quantity, 'Quantity cannot be negative', ['qty'])]
✅ Summary Table
Attribute | Purpose | Common Use Case |
_name | Defines model name | New custom model |
_inherit | Extend existing model | Add fields to res.partner |
_inherits | Delegated model composition | Create student.record from partner |
_description | UI label for model | Label in developer tools |
_rec_name | Record display field | Show code instead of name |
_order | Default record order | Sort orders by date |
_sql_constraints | Enforce uniqueness or data checks | Ensure email is unique |
_auto | Auto-create DB table | Disable for views |
_table | Custom DB table name | Use log_entry_table |
_log_access | Audit fields like create_uid | Disable for performance or logs |
_check_company_auto | Ensure company consistency | Multi-company safety |
_register | Register model in Odoo | Disable for abstract base classes |
_transient | Auto-delete records | Used in wizards |
_constraints | Add Python-based validations | Validate field relationships |
These meta attributes offer powerful control over model behavior and structure. Use them carefully to follow Odoo design patterns and ensure clean, maintainable code.