When developing in Odoo, understanding the different field types is essential for creating robust and efficient models. Each field type serves a specific purpose, allowing developers to store, retrieve, and manipulate data effectively. In this post, we’ll explore all the field types in Odoo, their purposes, and examples of how to use them in your models. This guide will serve as a complete reference for Odoo developers.
1. Basic Field Types
These are the core fields used for basic data storage.
Char
- Purpose: Stores short text strings, such as names or titles.
- Example:
name = fields.Char(string="Name", required=True) - Use Case: For product names, customer titles, or short descriptions.
Text
- Purpose: Stores long text strings (multi-line).
- Example:
description = fields.Text(string="Description") - Use Case: For storing comments, notes, or detailed descriptions.
Float
- Purpose: Stores decimal numbers for precise calculations.
- Example:
price = fields.Float(string="Price") - Use Case: For product prices, percentages, or measurements.
Integer
- Purpose: Stores whole numbers.
- Example:
quantity = fields.Integer(string="Quantity") - Use Case: For quantities, counts, or ranking values.
Boolean
- Purpose: Stores True/False values, often displayed as a checkbox.
- Example:
is_active = fields.Boolean(string="Is Active", default=True) - Use Case: To indicate whether a record is active, enabled, or available.
Selection
- Purpose: Stores a value from a predefined list of options.
- Example:
status = fields.Selection( selection=[('draft', 'Draft'), ('confirmed', 'Confirmed')], string="Status", default='draft' ) - Use Case: For statuses, categories, or fixed choices.
2. Relational Field Types
These fields are used to define relationships between models.
Many2one (M2O)
- Purpose: Links the current model to another model (a many-to-one relationship).
- Example:
partner_id = fields.Many2one('res.partner', string="Customer") - Use Case: To link orders to customers or tasks to projects.
One2many (O2M)
- Purpose: Represents a one-to-many relationship.
- Example:
order_lines = fields.One2many('sale.order.line', 'order_id', string="Order Lines") - Use Case: To display child records, such as order lines in a sales order.
Many2many (M2M)
- Purpose: Creates a many-to-many relationship between models.
- Example:
tags = fields.Many2many('product.tag', string="Tags") - Use Case: For assigning multiple tags to a product or linking users to multiple groups.
3. Date and Time Fields
These fields handle dates and times.
Date
- Purpose: Stores dates without time.
- Example:
start_date = fields.Date(string="Start Date") - Use Case: For birth dates, event dates, or deadlines.
Datetime
- Purpose: Stores both date and time.
- Example:
meeting_time = fields.Datetime(string="Meeting Time") - Use Case: For appointments, logs, or time-sensitive activities.
4. Monetary Fields
Monetary
- Purpose: Stores monetary values linked to a currency field.
- Example:
amount = fields.Monetary(string="Amount", currency_field="currency_id") currency_id = fields.Many2one('res.currency', string="Currency") - Use Case: For prices, expenses, or financial transactions.
5. Binary Fields
Binary
- Purpose: Stores binary data, such as files or images.
- Example:
file = fields.Binary(string="Attachment") - Use Case: For storing documents, images, or other binary files.
6. Special Field Types
HTML
- Purpose: Stores rich-text (HTML-formatted) content.
- Example:
description_html = fields.Html(string="Description") - Use Case: For product descriptions, emails, or content requiring formatting.
JSON
- Purpose: Stores structured data in JSON format.
- Example:
metadata = fields.Json(string="Metadata") - Use Case: For storing dynamic or nested data structures.
Reference
- Purpose: Stores a reference to any model.
- Example:
reference = fields.Reference( [('res.partner', 'Partner'), ('res.users', 'User')], string="Reference" ) - Use Case: For linking records to multiple models.
7. Computed and Related Fields
Computed Fields
- Purpose: Fields that calculate their values dynamically.
- Example:
total = fields.Float(string="Total", compute="_compute_total") @api.depends('quantity', 'price') def _compute_total(self): for record in self: record.total = record.quantity * record.price - Use Case: For totals, balances, or dynamically derived values.
Related Fields
- Purpose: References a field from a related model.
- Example:
partner_email = fields.Char(related="partner_id.email", string="Customer Email") - Use Case: To display related information without duplicating data.
8. Miscellaneous Fields
Password
- Purpose: Stores password values securely.
- Example:
password = fields.Char(string="Password") - Use Case: For authentication or user credentials.
Priority
- Purpose: Often used to represent priority with pre-defined icons.
- Example:
priority = fields.Selection( selection=[('0', 'Low'), ('1', 'Normal'), ('2', 'High')], string="Priority", default='1' ) - Use Case: For tasks, tickets, or service requests.
Conclusion
Odoo offers a wide range of field types to accommodate all kinds of data storage and manipulation. Whether you’re building a simple application or a complex ERP system, understanding these field types allows you to design efficient and scalable models.
By mastering these field types, you can unlock the full potential of Odoo’s ORM framework and take your development skills to the next level.