首页 文章

在odoo v8中打印qweb-report时遇到问题

提问于
浏览
0

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

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>

如果数据['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'

update in py code

我刚刚换到了

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
    }

列表而不是_341726中的字典......

1 回答

  • 0

    我不确定我认为您使用数据存在冲突['form'] . 在abstract_report.py文件中提到了错误中引用的文件 . 如果您按照Odoo Reports的文档进行操作,他们会像这样描述覆盖render_html函数 . 而不是在qweb中使用数据['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,你会注意到他们使用了数据['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>
    

相关问题