Transient models or often called wizards are powerful tools in Odoo 19 that enable developers to create temporary multi-step user interactions without cluttering the database. Understanding how these models work is essential for building efficient, user friendly Odoo applications.
What Are Transient Models?
Transient models in Odoo 19 are temporary database models that inherits from models.TransientModel instead of the standard models.Model. These wizard models store data temporarily and are automatically cleaned up by Odoo's garbage collection system, making them perfect for handling user input in pop-ups dialogs and multi step processes.
Key Characteristics of Odoo 19 Wizard Models
1. Automatic Data Cleanup
Odoo 19 Implements an intelligent garbage collection mechanism for transient models. By default, records olrder than one hour are automatically removed from the database. This prevents your PostgreSQL database from becoming bloated with temporary wizard data.
2. Memory-Efficient Design
Transient models use minimal database resources since they don't require the extensive indexing and constraint management that permanent models need. This makes wizard operations significantly faster, particularly in large Odoo 19 installations.
Creating a Wizard Transient Model in Odoo 19
Building a wizard in Odoo 19 involves several key steps. Here's how to create a functional wizard model from scratch.
Step 1: Define the Wizard Model Class
Create your wizard model by inheriting from models.TransientModel. This tells Odoo 19 to treat the model as temporary data storage.
python
from odoo import models, fields, api
class SaleOrderWizard(models.TransientModel):
_name = 'sale.order.wizard'
_description = 'Sales Order Processing Wizard'
order_id = fields.Many2one('sale.order', string='Sale Order', required=True)
discount_percent = fields.Float(string='Discount Percentage', default=0.0)
apply_to_all = fields.Boolean(string='Apply to All Lines', default=False)
notes = fields.Text(string='Additional Notes')
Step 2: Implement Wizard Action Methods
python
def action_apply_discount(self):
self.ensure_one()
if self.apply_to_all:
for line in self.order_id.order_line:
line.discount = self.discount_percent
else:
# Apply selective discount logic
pass
return {'type': 'ir.actions.act_window_close'}
Step 3: Create the Wizard View
xml
<record id="view_sale_order_wizard_form" model="ir.ui.view">
<field name="name">sale.order.wizard.form</field>
<field name="model">sale.order.wizard</field>
<field name="arch" type="xml">
<form string="Apply Discount">
<group>
<field name="order_id"/>
<field name="discount_percent"/>
<field name="apply_to_all"/>
<field name="notes"/>
</group>
<footer>
<button string="Apply" type="object" name="action_apply_discount" class="btn-primary"/>
<button string="Cancel" class="btn-secondary"/>
</footer>
</form>
</field>
</record>
Advanced Wizard Patterns in Odoo 19
Context-Based Wizard Invocation
Passing context data to wizards enables dynamic behavior based on the source action. This is essential for wizards that operate on selected records in tree views.
python
def action_open_wizard(self):
return {
'name': 'Process Records',
'type': 'ir.actions.act_window',
'res_model': 'my.wizard.model',
'view_mode': 'form',
'target': 'new',
'context': {
'default_order_id': self.id,
'active_ids': self.ids,
}
}
Default Value Population
Odoo 19 wizards can automatically populate fields using default methods and context values. This improves user experience by pre-filling known information.
python
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
active_id = self.env.context.get('active_id')
if active_id:
order = self.env['sale.order'].browse(active_id)
res['order_id'] = order.id
res['discount_percent'] = order.partner_id.discount_default
return res
Conclusion
Wizard transient models are fundamental building blocks in Odoo 19 development. They provide a clean, efficient way to handle temporary user interactions without database overhead. By following the patterns and best practices outlined in this guide, you can create intuitive, performant wizards that enhance your Odoo applications.
Whether you're building simple confirmation dialogs or complex multi-step processes, understanding transient model architecture ensures your Odoo 19 modules remain maintainable, scalable, and user-friendly. Start implementing wizards in your next Odoo project to streamline user workflows and improve overall application quality.
