In this blog, we are going to know about the different types of actions in odoo 13. Actions are done by the users and the system gives some response based on user action, these responses are actually known as actions.
There are a number of actions in Odoo13
1) Server Actions (ir.actions.server)
2) Window Actions (ir.actions.act_window)
3) URL Actions (ir.actions.act_url)
4) Client Actions (ir.actions.client)
5) Report Actions (ir.actions.report)
6) Automated Actions (ir.cron)
These actions are stored in the database and when we call a method like a button method which refers to actions it returns directly as dictionaries.
You can see all user-defined actions from Settings -> Technical -> Actions.
In the action creation form, two mandatory char fields are there.
Name: Name of the action, it may display on the client interface.
Action Type: Where we define the type or category of the current action.
These are the two mandatory attributes and we have a number of optional argument are like,
res_model, binding_model_id, binding_view_types, view_id, view_ids, view_mode, view_type, search_view_id, state, code, domain, context, target etc.
1. Server Actions (ir.actions.server)
This odoo action triggers the server code. It is helped to execute various types of actions automatically.
The available server actions are :
* Execute Python Code: Written python code that will be executed
* Create a new Record: Create a new record with values.
* Write on a Record: Update the record values.
* Execute several actions: Which trigger several server actions.
model_id : Model refers to this action.
binding_model_id : It defines which model is bound to this action.
binding_view_types : Mostly list / form is used to represent this field.
state: Options are,
* code: Executes python code given in the argument of code.
* object_create: It creates a new record.
* object_write: Update the current record.
* multi: Executes several actions given in the argument of child_ids.
code: Used to write python code and execute this code when the action is occur.
Example
xml:
<record id="action_server" model="ir.actions.server"><field name="name">Test Server Action</field><field name="model_id" ref="model_test_model"/><field name="binding_model_id" ref="model_test_model"/><field name="binding_view_types">list</field><field name="state">code</field><field name="code">action = model.action_test_code()</field></record>
This will execute the method action_test_code() in the model test model.
For more details you can refer How to Create Server Actions in Odoo 13 blog.
2. Window Actions (ir.actions.act_window)
This is the most common type action, it triggers a different view of the model and in other words it is used to visualize the models with different types of views.
xml:
<record id="action_test_window_action" model="ir.actions.act_window"><field name="name">Test Window Action</field><field name="res_model">test.model</field><field name="view_mode">tree,form</field><field name="view_ids" eval="[(5, 0, 0),(0, 0, {'view_mode': 'tree', 'view_id': ref('view_test_model_tree')}),(0, 0, {'view_mode': 'form', 'view_id': ref('view_test_model_form')})]"/><field name="context">{}</field><field name="help" type="html"><p class="oe_view_nocontent_create">Create</p></field></record>
res_model : Model for representing this action window.
res_id : It is used to pass the active id of res_model.
view_mode : List of view types separated by commas(no spaces), ie, kanban, tree, form.
view_id : Provide the default view type will be open when the action is executed. If there is nothing it will open the window with the view of first type in the view_mode.
view_ids : We can define specific views for specific view_mode.
search_view_id : Setting the default search view for the object.
context : Used to pass additional context data passed to view.
domain : Condition used to filter the data passed to view.
target : We can define different options for target windows.
* new : It opens a window with a new address and edit mode(as when creating wizards) .
* current : It opens the regular form views.
* fullscreen : It will open the window in fullscreen mode.
* main : Without breadcrumb, it opens the main content area.
* inline : It will open the window in edit mode.
Example: A wizard will open while clicking a button.
py:
return {'name': _('Test Window Action'),'view_mode': 'tree, form','res_model': 'test.model','type': 'ir.actions.act_window','target': 'new','res_id': new_wizard.id,'context': {},}
3. URL Actions (ir.actions.act_url)
This action used to open a URL(website/web page).
xml:
<record id="action_website" model="ir.actions.act_url"><field name="name">Website</field><field name="url">https://www.google.com/</field><field name="target">self</field></record>
py :
return {'name': _("Visit Webpage"),'type': 'ir.actions.act_url','url': https://www.google.com/,'target': 'new',}
There is two important customized fields are,
url : The opened url when activating the action.
target : Types of target windows for url action.
* self : It opens the current window with current content.
* new : It redirects to the address with a new window.
4. Client Actions (ir.actions.client)
It triggers the client implemented actions.One can create client actions as per our needs. Basically these actions are defined as menu_items in xml and it mapped to a widget.
Example:
Xml:
<record id="example_client_action" model="ir.actions.client"><field name="name">Pdf Report</field><field name="tag">pdf_report_page</field></record><menuitem id="example_menuitem" name="Reporting" parent="report_menu"action="example_client_action"/>
Then we need to create a widget in js with the name pdf_report_page.
tag: Which is an arbitrary string for which the client can identify the action.
params : It is a python dictionary with additional data to the client.
target: Options are,
* current : It opens the main content area.
* new: It will open a window like a dialog/popup.
* fullscreen: It will open a window in fullscreen mode.
* main: It will open the main content area without breadcrumbs.
5. Report Actions (ir.actions.report)
It mainly triggers the printing of Report action.
<record id="pos_invoice_report" model="ir.actions.report"><field name="name">Invoice</field><field name="model">pos.order</field><field name="report_type">qweb-pdf</field><field name="report_name">point_of_sale.report_invoice</field><field name="print_report_name">'Invoice - %s' % (object.name)</field></record>
name : Name of report action.
Model : The model of the report will be about.
report_type : qweb-html and qweb-pdf (for HTML and for PDF reports )
report_name : Report name.
groups_id : Group allowed for the current report.
paperformat_id : Field to specify the paper format for the report. It is a many to many field.
attachment : Where we can define a python expression(name of the report).
attachment_use : It is set to true when the reports must be generated once and we can reprint the stored report instead of generating it every time.
Example: A button with ‘ir.actions.report’ type returns a dictionary. The name of the report referred to by report_name and data of the current record is stored in the variable named data.
def report_action(self, docids, data=None, config=True):context = self.env.contextif docids:if isinstance(docids, models.Model):active_ids = docids.idselif isinstance(docids, int):active_ids = [docids]elif isinstance(docids, list):active_ids = docidscontext = dict(self.env.context, active_ids=active_ids)data = self.read()[0]datas = {'ids': context.get('active_ids', []),'model': 'account.analytic.account','form': data}report_action = {'context': context,'data': datas,'type': 'ir.actions.report','report_name': self.report_name,'report_type': self.report_type,'report_file': self.report_file,'name': self.name,}
6. Automated Actions (ir.cron)
This action is used for triggering functions automatically at certain intervals. To know more about Automated Actions In Odoo blog.