Create custom excel report from wizard in odoo 15
I am trying to generate an excel report from wizard using report_xlsx
module but i am getting below error.
Traceback (most recent call last): File "/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py", line 76, in report_download response = self.report_routes( File "/home/kabeer/Projects/Odoo15ce/odoo/http.py", line 535, in response_wrap response = f(*args, **kw) File "/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py", line 32, in report_routes data.update(json.loads(data.pop("options"))) ValueError: dictionary update sequence element #0 has length 10; 2 is required
Here is what i did.
*.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="open_purchase_order_print_menu" model="ir.actions.report">
<field name="name">Open PO</field>
<field name="model">open.po</field>
<field name="report_type">xlsx</field>
<field name="report_name">transight.open_po</field>
<field name="report_file">transight.open_po</field>
<field name="print_report_name">'Purchase Order'</field>
<!-- <field name="binding_model_id" ref="purchase.model_purchase_order"/> -->
<field name="binding_type">report</field>
</record>
<record id="open_po_view_form" model="ir.ui.view">
<field name="name">open.po.form</field>
<field name="model">open.po</field>
<field name="arch" type="xml">
<form string="Open PO">
<group>
<field name="mode" required="1"/>
</group>
<group col="4">
<field name="purchase_order_id"
widget="many2many_tags"
attrs="{'invisible':[('mode','=','partnumber')]}"/>
<field name="partnumber" attrs="{'invisible':[('mode','=','po')]}"/>
</group>
<footer>
<button string="Print" name="action_print" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_open_po" model="ir.actions.act_window">
<field name="name">Open PO</field>
<field name="res_model">open.po</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem id="menu_open_po_action" name="Open PO" parent="mrp.menu_mrp_configuration" action="action_open_po" sequence="4"/>
</odoo>
*.py
from odoo import fields,models,api,_
class OpenPOXlsx(models.AbstractModel):
_name = 'report.transight.open_po'
_inherit = 'report.report_xlsx.abstract'
def generate_xlsx_report(self, workbook, data, partners):
print('data',data)
for obj in partners:
report_name = 'Excel Report test'
# One sheet by partner
sheet = workbook.add_worksheet(report_name[:31])
bold = workbook.add_format({'bold': True})
sheet.write(0, 0, 'Hi Am here', bold)
class OpenPO(models.TransientModel):
_name = 'open.po'
_description = 'OpenPO'
purchase_order_id = fields.Many2many('purchase.order',string='Purchase Order')
partnumber = fields.Char(string="Partnumber")
mode = fields.Selection([('partnumber','Part Number'),('po','Purchase Order')],string="Filter by")
def action_print(self):
if self.mode == 'po':
query = """my query %s """
self.env.cr.execute(query,(tuple(self.purchase_order_id.ids),))
datas = self.env.cr.dictfetchall()
print('data---------',datas)
# return self.env.ref('account_batch_payment.action_print_batch_payment').report_action(self, config=False)
return self.env.ref('transight.open_purchase_order_print_menu').report_action(self,data=datas)
The dictfetchall() method returns a list of dicts and report_action
is expecting data
(datas) to be a dict.
In previous Odoo versions, we pass record data using form
key, for example:
datas = {
'ids': active_ids,
'model': 'hr.contribution.register',
'form': self.read()[0]
}
To fix this issue, add the fetched values to datas:
values = self.env.cr.dictfetchall()
datas['values'] = values
Then use values
key in generate_xlsx_report
method
Make sure that datas
is a dictionary before calling report_action