In Odoo, models form the foundation of how data is structured, managed, and processed. They define how information is stored in the database, how it can be retrieved, and how it interacts with different business workflows. While most Odoo models are designed to store data permanently, there is a special kind of model that works differently from the Transient Model.
Transient Models are particularly useful when you need to store data temporarily, such as when guiding a user through a wizard or handling short-lived configuration inputs. In this blog, we’ll explore what a transient model is, how to create one, and how Odoo uses them for settings storage.
What is a Transient Model?
A Transient Model in Odoo is a model that stores temporary data. Unlike standard models (which inherit from models.Model), transient models inherit from models.TransientModel. This makes them short-lived data stored in them is automatically cleared after a certain time, usually when the user’s session ends or after a scheduled database cleanup.
- Transient models do not store data permanently in their database tables.
- Once a record is created, it will exist only for a short time before being automatically removed.
- This makes them ideal for processes where the data has no long-term value.
They are most commonly used in wizards, where user inputs are required for a process but don’t need to be stored permanently in the database.
How to Create a Transient Model in Odoo
Let’s go through a step-by-step example.
Step 1: Define the Transient Model
from odoo import models, fields
class MyTransientModel(models.TransientModel):
_name = 'my.transient.model'
_description = 'Temporary Data Storage Model'
name = fields.Char(string='Name')
temporary_data = fields.Text(string='Temporary Data')
This creates a transient model with two fields: name and temporary_data.
Step 2: Use the Transient Model in a Wizard
class MyWizard(models.TransientModel):
_name = 'my.wizard'
input_data = fields.Char(string='Input Data')
def action_confirm(self):
# Process the wizard data
print("Wizard data processed: ", self.input_data)
This wizard collects temporary user inputs and processes them without saving them permanently.
Transient Models in Odoo Settings
By default in Odoo, most Settings models are actually Transient Models. For example, the forms under Settings > General Settings or Technical Settings do not save their data permanently in the transient model’s table.
Instead, Odoo uses the ir.config_parameter table to persist settings values across sessions.
This means:
- The transient model acts as the interface for the form.
- When you click Save, the values are written into ir.config_parameter.
- Later, those values can be retrieved for use in the system.
Example: Saving and Retrieving Settings
from odoo import api, models
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param(
'my_module.secret_key', self.field_name
)
@api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
res.update(
secret_key=self.env['ir.config_parameter'].sudo().get_param(
'my_module.secret_key', default=''
)
)
return res
Auto-Cleanup of Transient Models
Odoo automatically removes transient records through its vacuum process. This ensures that temporary data doesn’t accumulate and bloat the database.
Transient models include the following configuration attributes:
- _transient_max_count = 0
The maximum number of transient records to keep. 0 means unlimited.
- _transient_max_hours = 1.0
The maximum lifetime (in hours) of transient records. Default is 1 hour. 0 means unlimited.
- _transient_vacuum()
The method that actually deletes old records when thresholds are exceeded. Cleanup is triggered automatically, but it only runs once every 5 minutes.
Example Cleanup Scenario
Suppose:
- _transient_max_hours = 0.2 (12 minutes)
- _transient_max_count = 20
And in your table, there are 55 rows:
- 10 rows created/changed in the last 5 minutes
- 12 rows created/changed between 5–10 minutes ago
- 33 rows older than 12 minutes
Cleanup behavior:
- Age-based cleanup keeps only the 22 most recent rows (from the last 12 minutes).
- Count-based cleanup further reduces the table by wiping out another 12 rows (not just 2), to avoid hitting the limit again immediately.
- The 10 most recent rows (last 5 minutes) are never deleted until they expire by age.
This mechanism ensures transient tables remain lightweight and self-cleaning.
Key Characteristics of Transient Models
- Data is temporary: Records are automatically removed after a short period.
- Session-bound: Ideal for short-lived processes like wizards.
- Not for permanent storage: Use ir.config_parameter if you need persistence.
- Lightweight: Prevents the database from being filled with unnecessary records.
- Default for Settings: All Odoo settings models are transient, but their values are persisted in ir.config_parameter.
- Auto-cleanup: Managed by _transient_max_hours, _transient_max_count, and _transient_vacuum().
Transient Models in Odoo are the right choice for handling short-lived, temporary data. They are perfect for wizards and quick operations where the data doesn’t need to stay in the database.
At the same time, Odoo cleverly combines them with ir.config_parameter to implement its Settings system allowing developers to define configuration fields in a transient model while saving values permanently behind the scenes.
By mastering transient models and knowing when to pair them with ir.config_parameter, you can design cleaner, more efficient workflows while keeping your database optimized.
To read more about Model Attributes in Odoo 17, refer to our blog Model Attributes in Odoo 17