从 odoo 15 中的向导创建自定义 excel 报告

Create custom excel report from wizard in odoo 15

我正在尝试使用 report_xlsx 模块从向导生成 excel 报告,但出现以下错误。

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

这是我所做的。

*.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)

dictfetchall() 方法 returns 一个字典列表和 report_action 期望 data (datas) 是一个字典。

在之前的Odoo版本中,我们使用form键传递记录数据,例如:

datas = {
  'ids': active_ids,
  'model': 'hr.contribution.register',
  'form': self.read()[0]
}  

要解决此问题,请将提取的值添加到数据中:

values = self.env.cr.dictfetchall()
datas['values'] = values

然后在generate_xlsx_report方法中使用values

在调用report_action

之前确保datas是一个字典