Fields define the structure of the module and how the data is being stored and controlled. Odoo provides a wide variety of field types to suit various business needs varying from simple text fields to different complex relational fields.
In this blog we will discuss about the most popular types of Odoo fields.
Basic Field Types
These field types contain basic data such as text, numbers, booleans, etc. The following are some of the commonly used basic field types:
| Field Type | Description | Example | Syntax |
|---|---|---|---|
| Char | Short text containing various characters | Emails, Name, Phone Numbers, Codes, etc. | name = fields.Char(string=”Name”) |
| Text | Longer text containing various characters | Descriptions, Notes, etc | notes = fields.Text(string=”Notes”) |
| Boolean | True/ False values | Record active, Toggle buttons, etc | active = fields.Boolean( string=”Active”) |
| Integer | Storing all integers or whole numbers | Quantity, Floors, Capacity, Age, etc | quantity = fields.Integer( string=”Quantity) |
| Float | Storing decimal numbers | Price, Weight, etc | price = fields.Float( string=”Price”) |
| Monetory | Storing all monetary values | Total amounts, Tax amounts , etc | amount_total = fields.Monetory( string=”Total Amount”) |
| Date | Storing value of date only | Date of Birth, Joining Date, etc | dob = fields.Date( string=”DOB”) |
| Datetime | Storing date and time values | Order Date, Time Off Start and End dates, etc | order_date = fields.Datetime( string=”Order Date”) |
| Selection | Storing different values mapped to corresponding keys and acts like dropdowns. | State, Type, Gender, etc. |
gender = fields.Selection( selection=[("male", "Male"), ("female", "Female"),], default="male") |
| Html | Store text that can be edited using odoo HTML editor | Email Bodies, Job Descriptions, etc. |
narration = fields.Html (string='Terms and Conditions') |
| Binary | Storing images or files | Attachments, Product Images, Employee Images, etc. | Image = fields.Binary( string=”Image”) |
Relational Field Types
The relational fields are used to define connections between different models. It is used for linking and displaying the data from another model. The following are some of the relational field types:
| Field Type | Description | Relation | Syntax |
|---|---|---|---|
| Many2one | Link the current model’s record to a single record in another model | Many to one | partner_id = fields.Many2one('res.partner') |
| One2many | Link the current model’s record to a multiple records in another model | One to many | order_lines = fields.One2many( ‘sale.order.line’) |
| Many2many | For creating a bidirectional relation between multiple records of both models | Many to many | tax_ids = fields.Many2many( account.tax’) |
Special & Advanced Fields
Some of the advanced and special purpose fields used in Odoo:
| Field Type | Description | Example | Syntax |
|---|---|---|---|
| Json | Stores JSON structure | Analytic Distribution, API data, etc. | api_data = fields.Json( string=’API Data’) |
| Reference | Polymorphic relation to any mode | Action, Reference, etc. | template_ref = fields.Reference(string='Template') |
| Property | Used for company-specific default values | Task Properties, Product Properties, etc. | product_properties = fields.Properties( 'Properties', definition='categ_id.product_properties_definition', copy=True) |
Different Field Properties To Define Fields
Below are some of the properties that are used to define a field through python at the time of field definition.
Related
The related field doesn’t establish connection between two models, but it rather uses an existing relation to fetch and display the data from another model.
Example:
Consider a field state in the sale order line that is to have the same value as the corresponding order’s state. Shown below is an example to define the field using the related option to achieve the functionality.
status = fields.Selection(related=’order_id.state’)
Computed
The computed option is used to define fields whose values are automatically calculated based on a Python method.
Example:
Consider a case where you want to compute the total price of sale order by adding the sum of all the price total in sale order line. Β Shown below is an example to define the field using the computed option to achieve the functionality.Β
amount_total = fields.Monetary(string=βAmount Totalβ, compute=βcompute_amount_totalβ)
Now you can define the corresponding python function βcompute_amount_totalβ to implement the logic.
Read-only
The readonly option is used if the user should not be able to modify the field values.Readonly has two options: True, False. The readonly property can be used conditionally by using it in xml while displaying the field.
Example:
Consider the creation date of records user should not be able to modify those records. Shown below is an example to define the field using the readonly option to achieve the functionality.
create_date = fields.Datetime(string=’Created Date’, readonly=True)
Required
The required option is used if the value of the field is mandatory. Required has two options: True, False.The required property can be used conditionally by using it in xml while displaying the field.
Example:
Consider the date of birth of employees which should be filled while creating employees. Shown below is an example to define the field using the required option to achieve the functionality.
dob = fields.Date(string=’DOB’, required=True)
Default
The default option is used to define fields which should have a default value passed to it automatically when a record is created.
Example:
Consider the date of order for a sale order, by default while creating the record todays date must be passed. Shown below is an example to define the field using the required option to achieve the functionality.
order_date = fields.Date(string=’Order Date’,default=fields.Date.today)
Leave a comment