Odoo ‐ Models - vec-ltd/odoo-docs GitHub Wiki

Transient Models

In my own understanding and experience transient models in Odoo are mainly used for displaying wizard popup messages and passing data to a main model since it does not create a record in the database.

Best Practice

For best practice I first create a wizards folder inside my module's folder to separate my transient models from my module's models

image

Class

Creating a transient model is similar to creating a normal model, it is created through a class and you specify the needed fields inside that model. The only difference is you pass a models.TransientModel instead of models.Model as a parameter.

class MessageWizard(models.TransientModel):
    _name = 'message.wizard'
    name = fields.Char(string='Name', required=True)

Form View

Creating the form view for the transient model. This the view that will be displayed if the transient model is used as a pop up window.

<record id="message_wizard_form" model="ir.ui.view">
        <field name="name">message.wizard.form</field>
        <field name="model">message.wizard</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <div class="oe_title">
                        <h1><field name="name" class="field_name" placeholder="Some Random Placeholder!"/></h1>
                    </div>
                </sheet>
                <footer>
                    <button name="action_save" string="Save" type="object" class="oe_highlight"/>
                    <button string="Discard" special="cancel" type="object"/>
                </footer>
            </form>
        </field>
</record>

Button Handling

In our form view we have two buttons a Save button named "action_save" and a Discard button with a special field equal to "cancel". If the Save button is clicked the function "action_save" will be called so we need to initialize that function in our transient class.

def action_save(self):
    #Put other codes here.......    

    # This closes the popup window/wizard form
    return {'type': 'ir.actions.act_window_close'}

Calling ir.actions.act_window - Python

So if you want to trigger or open your popup window/wizard form you just return this json/object. Usually this is done in your button function.

name (optional): The name of the window

res_model: The model of your transient model

views: Either a form or a tree

target (optional): whether the views should be open in the main content area (current), in full screen mode (fullscreen) or in a dialog/popup (new). Use main instead of current to clear the breadcrumbs. Defaults to current.

context (optional): additional context data to pass to the view. I usually use this to pass default value before the view is loaded.

More info here: https://www.odoo.com/documentation/16.0/developer/reference/backend/actions.html

return {
                'name': "Window Name",
                'type': "ir.actions.act_window",
                'res_model': "message.wizard",
                'views': [[False, "form"]],
                'target': "main",
                'context': {
                    default_name: this.name,
                },
            }

Calling ir.actions.act_window - JS

You can also trigger or open your popup window/wizard using OWL. So in order to do that you need the useService("action") from the /web/core/utils/hooks. Usually this also is done in your button function.

import { useService } from "@web/core/utils/hooks";

    setup(){
        this._super.apply(this, arguments);
        this.actionService = useService("action");
        // Getting the message
        this.name = this.props.name;
    },

ButtonFunction(){
            this.actionService.doAction({
                name: "Window Name",
                type: "ir.actions.act_window",
                res_model: "message.wizard",
                views: [[false, "form"]],
                target: "new",
                context: {
                    default_name: this.name,
                },
            });
}
⚠️ **GitHub.com Fallback** ⚠️