Odoo is a powerful and flexible ERP platform built on a robust object-relational mapping (ORM) layer. At the heart of every model (models.Model) in Odoo are fields. they define the structure of the data, how it is stored, displayed, and interacted with. Whether you're customising existing modules or building new ones, understanding Odoo field types is essential.
In this blog, we’ll walk through all the available field types in Odoo, explain how they work, and provide real-world usage examples.
A field in Odoo defines a column in the database and a corresponding element in the user interface. Fields control:
- The type of data stored (e.g., text, number, date)
- The behavior of the field (read-only, computed, default)
- How the field appears in views and forms
1. Basic Field Types
These fields handle standard data types like strings, numbers, and dates.
Field Type | Description | Example |
---|---|---|
Char | Single-line text | fields.Char(string="Name") |
Text | Multi-line text | fields.Text(string="Description") |
Boolean | Checkbox (True/False) | fields.Boolean(string="Is Active") |
Integer | Whole number | fields.Integer(string="Quantity") |
Float | Decimal number | fields.Float(string="Weight") |
Date | Date only | fields.Date(string="Start Date") |
Datetime | Date and time | fields.Datetime(string="Created On") |
Selection | Dropdown options | fields.Selection([ ('draft', 'Draft'), ('done', 'Done') ], string="Status") |
2. Relational Fields
These fields are used to create links between different models. Refer the video for complete demo
Many2one
Used to select a single record from another model (foreign key).
partner_id = fields.Many2one('res.partner', string="Customer")
One2many
Used to link one record to multiple records in another model.
order_line_ids = fields.One2many('sale.order.line', 'order_id', string="Order Lines")
Many2many
Used to create a many-to-many relationship between records.
tag_ids = fields.Many2many('crm.tag', string="Tags")
3. Computed Fields
Computed fields are automatically calculated using Python logic. Find below video for full demo.
total = fields.Float(string="Total", compute="_compute_total") @api.depends('price', 'quantity') def _compute_total(self): for rec in self: rec.total = rec.price * rec.quantity
Add store=True if you want the value saved in the database.
4. Related Fields
These fields fetch values from a related model.
customer_email = fields.Char(related='partner_id.email', string="Email", store=True)
Useful for referencing fields from related models directly in your current model.
5. Other Special Fields
Field | Use Case |
---|---|
Html | Store rich text content |
Binary | Upload and store files (PDF, Excel, etc.) |
Image | Store and resize images |
Monetary | Float with currency support |
Reference | Dynamically refer to any model |
Serialized | Store JSON data (technical use only) |
Example:
document = fields.Binary(string="Upload Document") price = fields.Monetary(string="Price", currency_field='currency_id')
6. Common Field Attributes
You can enhance fields using several attributes. Please find below video for full demo
Attribute | Purpose |
---|---|
string | Display label |
required=True | Make field mandatory |
readonly=True | Make field read-only |
default= | Set default value |
help= | Tooltip explanation |
index=True | Index the field in the database |
copy=False | Do not copy value when duplicating |
groups= | Restrict visibility based on user group |
Example:
price = fields.Float( string="Unit Price", required=True, default=0.0, help="The default selling price", copy=False )
7. Full Example: Defining a Product Model
class CustomProduct(models.Model): _name = 'custom.product' _description = 'Custom Product' name = fields.Char(string="Product Name", required=True) description = fields.Text(string="Description") price = fields.Float(string="Price") available = fields.Boolean(string="Available") category_id = fields.Many2one('product.category', string="Category") image = fields.Image(string="Product Image") total_stock = fields.Float(string="Total Stock", compute='_compute_total_stock', store=True) @api.depends('stock_line_ids.quantity') def _compute_total_stock(self): for rec in self: rec.total_stock = sum(line.quantity for line in rec.stock_line_ids)
Odoo fields are the core of any model or business logic. By mastering the various field types and their usage, you can build powerful, user-friendly modules that make full use of the Odoo framework. From basic data entry to complex relational logic, Odoo’s fields provide everything you need to create robust applications.