Odoo 10 Qweb Report, define filename for PDF file

2019-08-02 02:12发布

问题:

I have the following custom report:

<?xml version="1.0"?>
<odoo>
  <report
        id = "report_custom_sale_order"
        name = "custom_saleorder_v2.custom_report_saleorder"
        string = "Custom Quotation / Order"
        model = "sale.order"
        report_type = "qweb-pdf"
        report_name = "((object.number or 'SaleOrder').replace('/','')+'.pdf')"
        attachment_use = "False"
  />

</odoo>

I would like the PDF to be downloaded as SO003.pdf for sales order with reference/sequential SO003, and as SaleOrder.pdf for sales order in draft without assigned reference/sequential.

When I try to implement such behaviour with report_name I get the following error:

AssertionError: Element odoo has extra content: report, line 11

How report_name shall be used?


Following CZoellner comment I tried:

<?xml version="1.0"?>
<odoo>
    <report
        id = "report_custom_sale_order"
        name = "custom_saleorder_v2.custom_report_saleorder"
        string = "Custom Quotation / Order"
        model = "sale.order"
        report_type = "qweb-pdf"
        attachment_use = "False"
    />
    <record id="report_custom_sale_order" model="ir.actions.report.xml">
        <field name="print_report_name"><![CDATA[((object.number or 'SaleOrder').replace('/','')+'.pdf')]]></field>
    </record>
</odoo>

And I got the following error:

ParseError: "null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (523, 1, null, 1, 2018-02-13 17:17:20.385168, null, 2018-02-13 17:17:20.385168, ir.actions.report.xml, null, null, t, pdf, null, null, null, null, null, t, null, f, null, null, null, f, null, null, ((object.number or 'SaleOrder').replace('/','')+'.pdf')).
" while parsing /usr/lib/python2.7/dist-packages/odoo/custom_addons/custom_saleorder_v2/reports/sale_report.xml:12, near
<record id="report_custom_sale_order" model="ir.actions.report.xml">
    <field name="print_report_name">((object.number or 'SaleOrder').replace('/','')+'.pdf')</field>
</record>

Note: it is the same if I use object.name instead of object.number (which I think it is the right way of getting the reference/sequential of the document.

回答1:

The field print_report_name is not converted while using the special tag report in data XMLs. So just add the field with normal XML record afterwards:

<?xml version="1.0"?>
<odoo>
    <report
            id = "report_custom_sale_order"
            name = "custom_saleorder_v2.custom_report_saleorder"
            string = "Custom Quotation / Order"
            model = "sale.order"
            report_type = "qweb-pdf"
            attachment_use = "False"
    />
    <record id="custom_module_name.report_custom_sale_order" model="ir.actions.report.xml">
        <field name="print_report_name"><![CDATA[((object.number or 'SaleOrder').replace('/','')+'.pdf')]]></field>
    </record>
</odoo>

It's not converted in Odoo 10, but in Odoo 11 the field was added.



回答2:

You are using different field for report name and object. I have updated your code as following with the proper field names.

<?xml version="1.0"?>
<odoo>
  <report
        id = "report_custom_sale_order"
        name = "custom_saleorder_v2.custom_report_saleorder"
        string = "Custom Quotation / Order"
        model = "sale.order"
        report_type = "qweb-pdf"
        print_report_name = "((object.name or 'SaleOrder').replace('/','')+'.pdf')"
        attachment_use = "False"
  />

I hope this will help you.



回答3:

You can use print_report_name.

<?xml version="1.0"?>
<odoo>
    <report id = "report_custom_sale_order"
            name = "custom_saleorder_v2.custom_report_saleorder"
            string = "Custom Quotation / Order"
            model = "sale.order"
            report_type = "qweb-pdf"
            print_report_name = "((object.number or 'SaleOrder').replace('/','')+'.pdf')"
            attachment_use = "False" />
</odoo>