带向导的Odoo11自定义报告

时间:2018-09-03 07:49:58

标签: odoo odoo-11 odoo-wizard

我想使用自定义配置打印一些产品条形码。

现在,问题是,我已经创建了向导,但是当我单击“打印”按钮时,没有数据传输到报告中。

class BarcodeConfig(models.Model):
_name = 'report.barcode.print'

@api.model
def _default_product_line(self):
    active_ids = self._context.get('active_ids', [])
    products = self.env['product.product'].browse(active_ids)
    return [(0, 0, {'product_id': x.id, 'qty': 1}) for x in products]

product_line = fields.One2many('product.print.wizard', 'barcode_id', string="Products",
                               default=_default_product_line)

@api.multi
def print_report(self):
    data = self.read()[0]
    ids = [x.product_id.id for x in self.product_line]
    config = self.env['barcode.config'].get_values()
    data.update({
        'config': config
    })
    datas = {'ids': ids,
             'form': data
             }
    return self.env.ref('odoo_barcode.barcode_report').report_action(self,data=datas)

在上面的代码中,对于ids模型,我也有product.product

<template id="report_barcode_temp">
<t t-call="web.html_container">
    <t t-call="web.internal_layout">
        <div class="page">
            <h2>Report title</h2>
            <strong t-esc="docs"/>
            <strong t-esc="doc_ids"/>
            <span t-esc="data"/>
            <t t-foreach="docs" t-as="o">
                <span t-esc="o"/>
                <p>This object's name is
                    <span t-field="o.name"/>
                </p>
            </t>
        </div>
    </t>
</t>

在上面的代码中,我只是想打印出我所拥有的数据,但是我在文档中什么都没有。

<strong t-esc="docs"/>

此行打印product.product模型也是如此。但是ids不在这里,这就是我的问题。

有人可以帮我说一下为什么会这样吗?还是有其他方法可以实现这一目标?

1 个答案:

答案 0 :(得分:0)

我已经通过get_report_values方法解决了这个问题。

class classname(models.AbstractModel):
_name = 'report.modulename.templatename'

@api.model
def get_report_values(self, docids, data=None):
    # docids: pass from the wizard print button.
    records = self.env[objectname].browse(docids)
    return {
        'doc_ids': docids,
        'doc_model': objectname,
        'docs': records,
        'data': data,}