Odoo is an advanced and fully open-source ERP platform that equips developers to develop fully customized business software. Models and fields are at the core of all Odoo development activities. Models and fields determine the structure and relationships of data in the application. Models and fields are basic to all Odoo developers irrespective of whether they are working on developing a customized module or are developing a complex enterprise software application.
In this blog, we will take a look at how models and fields actually work in Odoo, and share some best practices for developers.
What Is a Model in Odoo?
In Odoo, a model represents a database table. Each model corresponds to a business object such as customers, sales orders, invoices, or custom entities like projects or events. Models are defined using Python classes and inherit from Odoo’s ORM (Object Relational Mapping) system.
Odoo models are usually located in the models directory of a custom module and extend the models.Model class.
Basic Structure of a Model
from odoo import models, fields
class LibraryBook(models.Model):
_name = 'library.book'
_description = 'Library Book'
- _name defines the technical name of the model (database table).
- _description provides a human-readable name for the model.
Once this model is installed, Odoo automatically creates a corresponding database table named library_book.
Types of Models in Odoo
Odoo supports different types of models based on how data is stored:
-
Model
-
TransientModel
-
AbstractModel
Model
Here it is corresponding directly to a database table. It is defined as ClassName(models.Model)
Transient Model
It is used for handling temporary data and does not need to be persisted in database. It is defined as ClassName(models.TransientModel)
Abstract Model
It is used as blueprints for other models and hence can not be on instantiated its own. It is defined as ClassName(models.AbstractModel)
For regular business data, models.Model is typically used.
What Are Fields in Odoo?
A Field defines the columns of a model’s database table. Fields determine data types, validation constraints, relationships, as well as display rules.
The fields are declared using class attributes with the help of fields module in Odoo.
Common Field Types in Odoo
Basic Fields
-
Char – For short text values
name = fields.Char(string="Book Name", required=True)
-
Text – For long text
description = fields.Text(string="Description")
3. Integer
copies = fields.Integer(string="Number of Copies", default=1)
-
Float
price = fields.Float(string="Price")
-
Boolean
available = fields.Boolean(string="Available", default=True)
-
Date and Datetime
publish_date = fields.Date(string="Publish Date")
created_on = fields.Datetime(string="Created On")
7.Selection - Selection fields restrict values to predefined options.
state = fields.Selection([
('draft', 'Draft'),
('published', 'Published'),
('archived', 'Archived')
], default='draft', string="Status")
Relational Fields in Odoo
Relational fields define relationships between models and are one of Odoo’s most powerful features.
Many2one (Many records → One record)
author_id = fields.Many2one(
'res.partner',
string="Author"
)
This creates a foreign key reference to another model.
One2many (One record → Many records)
chapter_ids = fields.One2many(
'library.chapter',
'book_id',
string="Chapters"
)
Defined using the target model and the inverse field.
Many2many (Many ↔ Many)
category_ids = fields.Many2many(
'library.category',
string="Categories"
)
Odoo automatically manages the relational table unless specified.
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.
Readonly
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 today's 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)
order_date = fields.Date(string=’Order Date’,default=fields.Date.today)
Adding Security for Models
The Security in Odoo Development Security is one of the most important perspectives of the Odoo development process. If you create a new model, it's by default not accessible to users unless proper security rules are defined.
Access Control Lists (ACLs)
The rights of access are defined in ir.model.access.csv files in the security directory.
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_library_book,library.book,model_library_book,base.group_user,1,1,1,1
Best Practices for Model and Field Creation
-
Use meaningful model names (module.model)
-
Always define _description
-
Avoid unnecessary stored computed fields
-
Use relational fields instead of duplicating data
-
Add help texts for better usability
-
Follow Odoo naming conventions (snake_case)
-
Defining proper security files
Leave a comment