Edit 2:
I watched the code again after a couple of days and realized it was 'actions=' instead of 'action=' that prevent server action to work.
So it's action = record.choose_between_reports() all the rest was fine.
Good morning, I want to render two different templates on condition (possibly avoid t-if) but I probably miss a step or something (Odoo version 15).
I declared 1 ir actions server and 2 ir actions report, on xml file.
Each report actions is related to a different template, and I want the server action to call python method to return one of the two report action on condition.
The python function called by the server action gets executed smoothly and ir action report on xml is correctly referenced, but when I use
'return action'
in python method doesn't render any template.
I thaught this would be a cool way to do it but maybe it is not possible and i should use
request.render() controller method or is it suppose to work the way i'm trying ? This is the complete code if some one can help me, thanks!
XML
<!-- Server action -->
<record id="act_server_filter_ddt_reports" model="ir.actions.server">
<field name="name">Render template on condition</field>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field name="model_id" ref="model_sale_order"/>
<field name="binding_model_id" ref="model_sale_order"/>
<field name="binding_type">report</field>
<field name="binding_view_types">form</field>
<field name="code">
actions = record.choose_between_reports()
</field>
</record>
<!-- Action report 1 -->
<record id="report_action_condition_1" model="ir.actions.report">
<field name="name">render template cond 1</field>
<field name="model">sale.order</field>
<field name="binding_model_id" ref="model_sale_order"/>
<field name="report_type">qweb-pdf</field>
<field name="report_name">module_name.template_1</field>
<field name="report_file">module_name.template_1</field>
<field name="binding_type">report</field>
<field name="paperformat_id" ref="module_name.paperformat_name"/>
</record>
<!-- Action report 2 -->
<record id="report_action_condition_2" model="ir.actions.report">
<field name="name">render template cond 2</field>
<field name="model">sale.order</field>
<field name="binding_model_id" ref="model_sale_order"/>
<field name="report_type">qweb-pdf</field>
<field name="report_name">module_name.template_2</field>
<field name="report_file">module_name.template_2</field>
<field name="binding_type">report</field>
<field name="paperformat_id" ref="module_name.paperformat_name"/>
</record>
Python
def choose_between_reports(self):
doc_ids = self.env.context.get('active_ids')
data = {
# datas
}
if condition:
action = self.env.ref('module_name.report_action_condition_1').report_action(docids=doc_ids, data={data}, config=False)
print(action)
return action
else:
action = self.env.ref('module_name.report_action_condition_2').report_action(docids=doc_ids, data={data}, config=False)
print(action)
return action