How to Call Different Python Actions Using JS
Have you ever thought about how we can call Python a new action or default action using JS? Or, can we call Python actions using JS? So in this blog, I am explaining how we can do that.
The Odoo uses OWL JS which is like a component-based JS. In OWL JS, different components exist along with hooks and methods. So, there is a method named ‘doAction’ which can be called using env.services.action.doAction or we can import the action component and call it the doAction method.
So, to call Python actions, we generally use the doAction method. This method can be used to call default actions as well as new custom actions. Let's see how it works and how we can use this.
Subscribe our newsletter to learn more about python!
How to call a Default Python Actions using JS
I am starting with the default Python action calling from JS. For default Python actions, we put an action ID and we can also add some additional context if we want to.
Here, I have created a button in a view, and upon clicking that button, I am calling this method ‘onClickButton’. In this, I am calling the doAction method and in that, I have passed the default action for opening the view of the Contact. We can pass some additional parameters like additionalContext for passing some extra context when calling a Python action.
The above code will redirect to Contact Default View. So, when we click on the button, the following will appear.
Check out the video for a closer look and additional information.
How to call Custom Python Action using JS
Now, let’s move to the custom python action. We will create a new Python action with the required parameters for this. We can choose to create any type of action. Currently, I am taking the example of the action type, “ir.actions.act_window”.
So, here is the code snippet of the new custom Python action that is showing “sale.order” tree view. So, here we can use the standard way of calling new python action. We can call form view or any type of view using this method.
This is how the view will look like when we call the above function at the click of a button some other way.
So, this is how you can call a Python action using JS.
Interested in learning more about Python and JavaScript? Check out our other blogs:
Get in touch with us!
Your email address will not be published.