Odoo 16 Studio Book - Python Code

There may be circumstances where we must carry out complicated processes, in these circumstances, we can execute Python code. If using other options in Action To Do, there are some limitations. When writing Python code, there are no limitations.

So In this documentation, we will be creating a new automation to confirm the sale order if the total is < 500 using Python. To do that, we should first open Studio and then choose Automations. To create a new automation, click the Create button.

odoo-studio

Complete all the required fields, and then in the Action to do field, choose to Execute Python code.

Now we can see how to use Python code space, so here we can write the Python code.

odoo-studio

We have added an if statement and called the action_confirm button function in this example.

In this area of Python code, the following variables are accessible:

  • Env: Odoo Environment on which the action is triggered.
  • Model: Odoo Model of the record on which the action is triggered; is a void recordset.
  • Record: record on which the action is triggered; may be void.
  • Records: recordset of all records on which the action is triggered in multi-mode; may be void.
  • Time, datetime, dateutil, timezone: useful Python libraries
  • Float_compare: Odoo function to compare floats based on specific precisions
  • Log: log(message, level='info'): logging function to record debug information in ir.logging table
  • UserError: Warning Exception to use with raise
  • Command: x2Many commands namespace
  • To return an action, assign: action = {...}
odoo-studio

To filter the records, we also added a domain. In this way, we can add Python code in automated action.

whatsapp
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