Enable Dark Mode!
a-complete-overview-of-fields-in-odoo-19.jpg
By: Mohammed Farhan

A Complete Overview of Fields in Odoo 19

Technical Odoo 19 Fields and widgets

Odoo 19 provides a wide range of field types to ensure efficient data handling, each offering specific features and functionalities. These fields are primarily classified into three main categories:

  1. Simple Types
  2. Relational Types
  3. Functional Types

Simple Types include basic data types such as Integer, Char, and String, which are used to store straightforward data values.

Relational Types define the relationships between different models or objects within Odoo. Common examples include Many2one, One2many, and Many2many, which help establish structured links between records.

Functional Types, on the other hand, are dynamic fields that are not stored in the database. Instead, they are computed in real-time based on the values of other fields in the view, making them ideal for displaying calculated or derived data.

Before defining fields in an Odoo model, it is necessary to import the required module as follows:

from odoo import fields, models

This import allows developers to define various field types within Odoo models efficiently.

Example:

class DummyModel(models.Model):
   _name = 'dummy.model'
   name = fields.Char(string="Name", required=True)
   dob = fields.Date(string="DOB", required=True, help="Date of Birth")

Field Type: Boolean

The Boolean field type in Odoo is used to store logical values — either True or False. It is typically implemented for features that involve binary choices, such as enabling or disabling options, marking a record as active or inactive, or indicating the completion status of a process.

Example:

is_active = fields.Boolean(string="Active")

Field Type: Char

The Char field type in Odoo is used to store short text strings or character data. It is commonly utilized for fields such as names, codes, titles, or any other text input that requires a defined length.

Parameter:

  • size – This parameter specifies the maximum length of the string. Any data entered beyond this limit will be automatically trimmed to fit the specified size.

Example:

name = fields.Char(string="Name", size=64)

Field Type: Text

The Text field type in Odoo is designed to store large blocks of text or lengthy descriptions. It is ideal for capturing detailed information such as comments, notes, product descriptions, or terms and conditions. Unlike the Char field, the Text field does not have a fixed length limit, allowing users to enter extensive content freely.

Example:

description = fields.Text(string="Description")

Field Type: Integer

The Integer field type in Odoo is used to store whole numeric values without any decimal points. It is typically applied in scenarios such as counting quantities, storing numerical identifiers, or representing ranking or sequence numbers.

By default, null values are not permitted in Integer fields. If no value is provided, the field automatically returns a default value of zero (0).

Example:

quantity = fields.Integer(string="Quantity")

Field Type: Float

The Float field type in Odoo is used to store decimal or floating-point numeric values. It is commonly utilized for fields involving prices, measurements, percentages, or any data that requires decimal precision.

Similar to the Integer field, null values are not supported in a Float field. If no value is assigned, it automatically returns 0.0 as the default.

Parameter:

  • digits – This parameter defines the precision and scale of the number.
    • Precision refers to the total number of significant digits (both before and after the decimal point).
    • Scale represents the number of digits after the decimal point.

Example:

order_date= fields.Date(string="Order Date" default=fields.Date.context_today)

Field Type: Datetime

The Datetime field type in Odoo is used to store both date and time values in a single field. It is commonly applied in cases such as recording creation dates, scheduling appointments, setting deadlines, or tracking timestamps where both date and time are important.

Odoo provides several helper methods for handling Datetime fields:

  • context_timestamp(): Returns the current date and time string based on the user’s timezone.
  • now(): Returns the current system date and time string, independent of timezone.
  • from_string(): Converts a string value into a datetime.datetime() object.
  • to_string(): Converts a datetime.datetime() object into a string format suitable for storage.

Example:

start_time = fields.Datetime(string="Start Time", default=fields.Datetime.now)

Field Type: Binary

The Binary field type in Odoo is used to store files or binary data, such as images, documents, or any other encoded content. The data is stored in a base64-encoded format within a column of type bytea in the database.

This field is useful when you need to attach files directly to records, for example, uploading profile pictures, invoices, or PDF documents.

Example:

attachment = fields.Binary(string="Attachment")

Field Type: Selection

The Selection field type in Odoo is used to store text values, but its primary purpose is to provide a dropdown-style selection widget in the user interface. It allows users to choose one option from a predefined list of choices.

The field accepts a parameter called selection, which can be defined as either:

  • A list of tuples, where each tuple contains a value and its corresponding label.
  • A callable function name that takes a recordset as input and returns a list of tuples dynamically.
  • Parameter:

  • selection: A list of tuples or a callable that defines the available options.
  • size: The parameter size=1 is mandatory when using integer indexes in the selection list.

Example:

status = fields.Selection(
   selection=[
       ('draft', 'Draft'),
       ('confirmed', 'Confirmed'),
       ('done', 'Done')
   ],
   string="Status",
   default='draft'
)

Field Type Extension: selection_add

When extending an existing model in Odoo, you can use the keyword argument selection_add to add new options to an already defined Selection field. This is particularly useful when you want to customize or enhance existing functionality without overriding the original selection list.

The selection_add parameter accepts a list of tuples, where each tuple represents a new key-value pair to be added to the existing selection.

Example:

status = fields.Selection(
   selection_add=[
       ('cancelled', 'Cancelled'),
       ('on_hold', 'On Hold')
   ]
)

Field Type: Reference

The Reference field type in Odoo is used to store a dynamic reference to any model and a specific record within that model. It allows linking a field to different models based on the data requirements, making it flexible for use cases where a record can be related to multiple possible models.

Parameter:

  • selection: A list of tuples or a callable function name that takes a recordset as input. This defines the available models that can be referenced. Each tuple typically contains the model’s technical name and its display label.

Example:

document_reference = fields.Reference(
   selection=[
       ('res.partner', 'Partner'),
       ('sale.order', 'Sales Order'),
       ('purchase.order', 'Purchase Order')
   ],
   string="Document Reference"
)

Field Type: Html

The Html field type in Odoo is used to store and display HTML-formatted content. It is primarily utilized for fields that require rich text input, such as descriptions, email templates, or website content. When used in the user interface, it appears as an HTML widget that supports formatted text, images, links, and other HTML elements.

Example:

notes = fields.Html(string="Notes")

Field Type: Monetary

The Monetary field in Odoo is specifically designed to store monetary or currency values. It is a specialized type of Float field that works in conjunction with a currency field to handle amounts in different currencies. This ensures accurate representation of financial data, including proper formatting and rounding according to the associated currency.

Parameters:

  • currency_field: Specifies the field in the model that contains the currency for this monetary value.
  • digits: Defines the precision and scale for the monetary value, controlling the total number of digits and the number of decimal places.

Example:

total_amount = fields.Monetary(
   string="Total Amount",
   currency_field='currency_id',
   digits=(12, 2)
)
currency_id = fields.Many2one('res.currency', string="Currency")

Field Type: Many2one

The Many2one field type in Odoo is used to create a relationship between the current model and another model, effectively linking each record in the current model to a single record in the related (parent) model. This is one of the most commonly used relational field types in Odoo, typically used for references such as customer, product, or company relationships.

Parameters:

  • comodel_name: Specifies the name of the related model to which the field is linked.
  • delegate: If set to True, the fields of the target model become directly accessible from the current model, allowing seamless access to related model data without redefining the fields.

Example:

customer_id = fields.Many2one(
   comodel_name='res.partner',
   string="Customer",
   delegate=False
)

Field Type: One2many

The One2many field type in Odoo is used to create a relationship from the current model to multiple records in another model. It essentially represents a one-to-many relationship, where a single record in the current model can be associated with many records in the related model. This field is commonly used for linking parent records to their child records, such as a sales order to its order lines or a project to its tasks.

Parameters:

  • comodel_name: Specifies the name of the related model that contains the multiple records.
  • inverse_name: Refers to the field in the related model that defines the Many2one relationship back to the current model.

Example:

order_line_ids = fields.One2many(
   comodel_name='sale.order.line',
   inverse_name='order_id',
   string="Order Lines"
)

Field Type: Many2many

The Many2many field type in Odoo is used to establish a many-to-many relationship between two models. This allows multiple records in the current model to be linked to multiple records in another model, making it suitable for scenarios such as assigning multiple tags to a product or linking multiple users to multiple projects.

Parameters:

  • comodel_name: Specifies the name of the related model.
  • relation: Defines the name of the intermediary relational table that stores the associations.
  • column1: Specifies the name of the column in the relational table that references the current model.
  • column2: Specifies the name of the column in the relational table that references the related model.

Example:

tag_ids = fields.Many2many(
   comodel_name='product.tag',
   relation='product_tag_rel',
   column1='product_id',
   column2='tag_id',
   string="Tags"
)

Common Field Parameters in Odoo

Odoo fields support several common parameters that enhance their functionality and control how they behave in the system. These parameters can be applied across different field types to manage data, enforce rules, and customize the user interface.

  • help: Provides a descriptive message explaining how the field should be used, which appears as a tooltip in the UI.
  • ondelete: Determines how deletions in a related record are handled. Possible values include 'restrict', 'no action', 'cascade', 'set null', and 'set default'.
  • readonly: If set to True, the field cannot be edited by users.
  • required: When True, this parameter ensures that the field cannot be left empty before saving a record.
  • size: Defines the size of the field in the database. It can be applied to character fields (Char) or integer fields to limit the maximum length or value.
  • string: Sets the label for the field that is displayed in forms and views.
  • translate: If True, the field content will be translatable, allowing multilingual support.
  • invisible: When set to 1 (or True), the field is hidden from the view in the user interface.
  • relation: Specifies a related table for relational or functional fields, defining which model the field is linked to.
  • compute: Refers to a function that calculates the field’s value dynamically. The parameter takes the name of the function that performs the computation.

Example:

total_amount = fields.Float(
   string="Total Amount",
   readonly=True,
   compute="_compute_total",
   help="This field shows the total amount of all order lines."
)

In Odoo 19, fields and their parameters are the building blocks of every model. Understanding how different field types store data and how parameters control their behavior allows developers to create clean, reliable, and user-friendly applications. With proper field design, Odoo modules become easier to maintain, more efficient, and better suited to real business needs.

To read more about What are Automatic & Reserved Fields in Odoo 18, refer to our blog What are Automatic & Reserved Fields in Odoo 18.


Frequently Asked Questions

Why are relational fields important in Odoo?

Relational fields connect models together, allowing records to share structured relationships. They help maintain organized data and enable smooth interaction between different business objects.

What is the purpose of field parameters in Odoo?

Field parameters define how a field behaves in the system. They control validation, visibility, computation, and user interaction, making fields more functional and adaptable.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message