Enable Dark Mode!
how-to-create-and-use-window-actions-in-odoo-19.jpg
By: Amrithesh K

How to Create and Use Window Actions in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

In Odoo, Window Actions are one of the most essential types of actions used to control the interface and display views for different business models. These actions define how and which data views (form, list, kanban, etc.) are displayed to the user when performing a task such as opening a menu, clicking a button, or navigating between records.

In this blog, we’ll explore how to create and use window actions in Odoo 19, understand their key parameters, and demonstrate how they can be implemented using both XML and Python code.

Understanding Window Actions in Odoo 19

Window Actions are represented by the model ir.actions.act_window.

They are responsible for displaying records of a model in one or more views, such as list, form, kanban, or pivot, based on defined filters, context, and configurations.

A window action can either be:

  • Defined in XML (and linked to menus or buttons), or
  • Returned from Python methods (e.g., button or server actions).

Key Attributes of Window Actions

  • type: Specifies the type of action. For window actions, it is always "ir.actions.act_window".
  • res_model: The model on which the action operates (e.g., "res.partner", "sale.order").
  • view_mode: Defines the display order and types of views (e.g., "list,form", "kanban,form").
  • views: A list of tuples specifying the view ID and type (used to define custom view sequences).
  • domain: A filter applied to limit which records are shown.
  • context: Passes extra parameters or default values when the action is triggered.
  • name: The title that appears in the Odoo interface for the action.
  • target: Determines where the view will open:
    • current – Opens in the same window (default).
    • new – Opens as a popup window.
    • main –It is used to open the main content area without breadcrumbs.
  • res_id: Opens a specific record in form view.
return {
   "name": _("Contacts"),
   "type": "ir.actions.act_window",
   "res_model": "res.partner",
   "domain": [("customer_rank", ">", 0)],
   "view_mode": "list",
   "views": [(False, "list"), (False, "form")],
   "context": {"create": False, "delete": False},
}

The above action opens all customer records (res.partner) showing both list and form views, filtered to display only customers with a customer rank greater than zero.

Creating Window Actions via XML

You can define window actions in XML and associate them with menu items to make them accessible from the user interface.

Step 1: Define the Views

<record id="res_partner_custom_view_form" model="ir.ui.view">
    <field name="name">res.partner.custom.view.form</field>
    <field name="model">res.partner</field>
    <field name="arch" type="xml">
        <form string="Contact">
            <sheet>
                <group>
                    <group>
                        <field name="name"/>
                        <field name="email"/>
                    </group>
                </group>
            </sheet>
        </form>
    </field>
</record>

Step 2: Define the Window Action

<record id="action_contacts_custom" model="ir.actions.act_window">
   <field name="name">Custom Contacts</field>
   <field name="res_model">res.partner</field>
   <field name="view_mode">list,form</field>
</record>

This window action opens the Custom Contacts records using the simple list and form views defined earlier, displaying only the Name and Email fields.

Step 3: Link the Action to a Menu Item

<menuitem id="menu_contacts_custom"
   name="Custom Contacts"
   parent="menu_contacts"
   action="action_contacts_custom"
   sequence="10"/>

This makes your action accessible through the Contacts > Custom Contacts menu.

Triggering Window Actions from Python

Window actions can also be returned from Python methods, typically through buttons or custom functions.

Returning a Window Action from a Button

from odoo import models, fields
class SaleOrder(models.Model):
   _inherit = "sale.order"
   def action_open_customer_orders(self):
       self.ensure_one()
       return {
           "type": "ir.actions.act_window",
           "name": "Customer Orders",
           "res_model": "sale.order",
           "view_mode": "list,form",
           "domain": [("partner_id", "=", self.partner_id.id)],
           "context": {"default_partner_id": self.partner_id.id},
           "target": "current",
       }

Button in XML

<button name="action_open_customer_orders"
       type="object"
       string="View Customer Orders"
       class="btn-primary"/>

When the button is clicked, it opens all sales orders related to the current customer.

Conclusion

In Odoo 19, Window Actions continue to play a crucial role in controlling how users interact with data models. They are flexible, reusable, and can be defined either in XML or dynamically via Python methods. Whether you’re opening a list of records, prefiltering data, or launching a pop-up form, window actions provide the foundation for building smooth and intuitive workflows in Odoo.

To read more about What are the Types of Actions in Odoo 18, refer to our blog  What are the Types of Actions in Odoo 18


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message