0

我正在尝试使用report_xlsx模块从向导生成一个 excel 报告,但我遇到了错误。

回溯(最近一次通话最后):文件“/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py”,第 76 行,report_download response = self.report_routes(文件“/home/kabeer/ Projects/Odoo15ce/odoo/http.py”,第 535 行,在 response_wrap 响应 = f(*args, **kw) 文件“/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py” ,第 32 行,在 report_routes data.update(json.loads(data.pop("options"))) ValueError: 字典更新序列元素 #0 的长度为 10;需要 2

这就是我所做的。

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

1 回答 1

1

dictfetchall ()方法返回一个字典列表,并report_action期望data(datas) 是一个字典。

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

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

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

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

然后使用valueskey in generate_xlsx_report方法

datas在调用之前确保这是一本字典report_action

于 2022-01-17T13:35:02.767 回答