【问题标题】:Getting issue with printing qweb-report in odoo v8在 odoo v8 中打印 qweb-report 时遇到问题
【发布时间】:2016-09-09 03:21:00
【问题描述】:

下面是我用来从向导打印报告的函数代码..

class manager_monthly_salary_report_wizard(osv.osv_memory):
    _name = "manager.monthly.salary.report.wizard"

    _columns = {
        'month': fields.integer("Month"),
        'year': fields.integer("Year"),
    }

    def check_report(self, cr, uid, ids, context=None):
        print "context -> ", context
        print "ids -> ", ids

        if context is None:
            context = {}

        self_read = self.read(cr, uid, ids)[0]

        if (self_read['month'] >= 12 or self_read['month'] <= 0) and len(str(self_read['year'])) != 4:
            raise osv.except_osv(_("Warning"),
                                 _("You have invalid month / year ! please select correct one ... "))
        else:
            print "working normally ... "

        if self_read['month'] in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
            month_year = str("0" + str(self_read['month']) or self_read['month'])
        elif self_read['month'] in [10, 11, 12]:
            month_year = str(self_read['month'])

        month_year = month_year + " - " + str(self_read['year'])
        print "month_year  ", month_year

        departments = []
        office_staff = []

        for deprt in first_level_department_brws:
            office_staff_ids = empl_obj.search(cr, uid, [('department_id', '=', deprt.id), '|', ('manager', '=', True), ('office_staff', '=', True)])
            office_staff_brw = empl_obj.browse(cr, uid, office_staff_ids)
            print " office_staff_brw ", office_staff_brw

            department = {
                'name': deprt.name,
                'office_staff': office_staff
            }
            for dep_employee in office_staff_brw:

                office_staff.append({
                    'name': dep_employee.name,...
                })
                department['office_staff'] = office_staff
                print "office_staff -> ", office_staff

            office_staff = []
            departments.append(department)
            print "Departments-> ", office_staff
            department = {}
            print "\n"

        data = {
            'ids': context.get('active_ids', []),
            'model': context.get('active_model', 'ir.ui.menu'),
            'form': departments
        }
        print "data -> ", data
        print "__end__\n\n"

        return self.pool['report'].get_action(cr, uid, [], 'custom_module.my_report', data=data, context=context)

报告 xml 文件看起来像 ..

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="my_report">
            <t t-call="report.html_container">
                <div class="page">
                    <!--header -->
                    <!--<t t-call="report.internal_layout">-->
                    <!--content-->

                    <div class="row">
                        <span >"====="</span>
                        <span t-esc="Departments"/>
                        <t t-esc="data"/>

                        <t t-set="model" t-value="data['model']"/>
                        <t t-set="data" t-value="data['form']"/>
                        <t t-esc="data"/>
                        <span >"++++++"</span>
                    </div>
                </div>
            </t>
        </template>
    </data>
</openerp>

如果 data['form'] 中的 departmens 是一种字典,我可以打印,但现在我已更改为列表,并且在打印报告时出现以下错误

Traceback (most recent call last):
  File "/home/demo/project/odoo/odoo_8/addons/report/controllers/main.py", line 120, in report_download
    response = self.report_routes(reportname, converter='pdf', **dict(data))
  File "/home/demo/project/odoo/odoo_8/openerp/http.py", line 410, in response_wrap
    response = f(*args, **kw)
  File "/home/demo/project/odoo/odoo_8/addons/report/controllers/main.py", line 65, in report_routes
    pdf = report_obj.get_pdf(cr, uid, docids, reportname, data=options_data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/report.py", line 192, in get_pdf
    html = self.get_html(cr, uid, ids, report_name, data=data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/report.py", line 167, in get_html
    return particularreport_obj.render_html(cr, uid, ids, data=data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/abstract_report.py", line 35, in render_html
    if data and data.get('form', {}).get('landscape'):
AttributeError: 'list' object has no attribute 'get'

更新 py 代码

我刚改成

    data = {
        'ids': context.get('active_ids', []),
        'model': context.get('active_model', 'ir.ui.menu'),
        'form': {'departments': departments}
    }

报告正在被打印出来......问题是如果我使用,他们为什么不能直接打印报告

    data = {
        'ids': context.get('active_ids', []),
        'model': context.get('active_model', 'ir.ui.menu'),
        'form': departments
    }

data['form'] 中的列表而不是 dict ... ??

【问题讨论】:

    标签: report odoo odoo-8 qweb


    【解决方案1】:

    我不确定,但我认为您在使用 data['form'] 时存在冲突。它在您的错误中引用的 abstract_report.py 文件中提到。如果您遵循Odoo Reports 的文档,他们会描述像这样覆盖 render_html 函数。不要在您的 qweb 中使用 data['form'] 尝试直接使用部门作为变量应该可用。

    class MyReport(models.AbstractModel):
        _name = 'report.custom_module.my_report'
    
        @api.multi
        def render_html(self, data=None):
            departments = []
            office_staff = []
    
            for deprt in first_level_department_brws:
                office_staff_ids = empl_obj.search(cr, uid, [('department_id', '=', deprt.id), '|', ('manager', '=', True), ('office_staff', '=', True)])
                office_staff_brw = empl_obj.browse(cr, uid, office_staff_ids)
                print " office_staff_brw ", office_staff_brw
    
                department = {
                    'name': deprt.name,
                    'office_staff': office_staff
                }
    
                for dep_employee in office_staff_brw:
                    office_staff.append({
                        'name': dep_employee.name, ...
                    })
    
                department['office_staff'] = office_staff
                print "office_staff -> ", office_staff
    
                office_staff = []
                departments.append(department)
                print "Departments-> ", office_staff
                department = {}
    
            report = self.env['report']._get_report_from_name('custom_addon.my_report')
            docs = self.env['custom_addon.model_name'].browse(context.get('active_ids', []))
    
            docargs = {
                'doc_model': report.model,
                'docs': docs,
                'departments': departments
            }
            return report_obj.render('custom_addon.my_report', docargs)
    

    尝试定义另一个 qweb varable var 或除数据以外的任何内容。有几个对数据的引用,我想知道是否存在冲突。您的原始方法看起来并没有完全错误,但是使用 odoo 已经在使用的变量名称(例如数据和表单)可能会发生冲突。如果您查看 /addons/report/abstract_report.py ,您会注意到他们使用了 data['form'] 但它看起来与您作为表单传递的内容完全不同。他们正在尝试确定表单是否是横向的,并且您的数据 ['form'] 似乎与您正在为其创建报​​告的记录相关。不是报表布局。

    <?xml version="1.0" encoding="utf-8"?>
    <openerp>
        <data>
            <template id="my_report">
                <t t-call="report.html_container">
                    <div class="page">
                        <!--header -->
                        <!--<t t-call="report.internal_layout">-->
                        <!--content-->
    
                        <div class="row">
                            <span >"====="</span>
                            <span>Departments</span>
                            <t t-raw="departments"/>
    
                            <span >"++++++"</span>
                        </div>
                    </div>
                </t>
            </template>
        </data>
    </openerp>
    

    【讨论】:

    • 谢谢,但这是我的格式data = {'form': [{'a':1,'b':[{..}]},{'a':1,'b':[{..}]},.. ],在报告中我只是尝试打印data['form'] 这是一个列表,而不是访问任何元素...&lt;t t-esc="data"/&gt; &lt;t t-set="model" t-value="data['model']"/&gt; &lt;t t-set="data" t-value="data['form']"/&gt; &lt;t t-esc="data"/&gt; 。 ..用你的例子我只是想打印form,然后我可以做&lt;t t-foreach="form" t-as="form-item"&gt; ...
    • 同样的错误...&lt;span t-esc="Departments"/&gt; &lt;!--&lt;t t-esc="data"/&gt;--&gt; &lt;span&gt;&lt;t t-raw="data['form']"/&gt;&lt;/span&gt; &lt;!--&lt;t t-set="model" t-value="data['model']"/&gt;--&gt; &lt;!--&lt;t t-set="data" t-value="data['form']"/&gt;--&gt; &lt;!--&lt;t t-esc="data"/&gt;--&gt;...
    • 不要使用 t-set 重新分配数据。在使用 t-set 重新分配之前对其运行 t-raw。
    • 我已经更新了我之前的评论,其中我评论了使用 t-set 重新分配的行 .... 但同样的错误 ...
    • 对不起,你在 python 文件中使用数据字典让我失望。如果您查看错误,则它是 odoo 的数据变量,而不是您文件中的数据变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多