custom report through python odoo 9

后端 未结 2 2175
醉酒成梦
醉酒成梦 2021-01-14 16:22

How to pass multiple module data to a QWeb report? Is there something similar to passing dictionary in rendering html from controller?



        
2条回答
  •  天命终不由人
    2021-01-14 17:05

    If you want to run specific code before your report prints or pass custom data to your template for rendering you can create an Abstract model which defines a render_html function so that your function will run when printing the report rather than the generic odoo function. This is referenced in the documentation HERE

    Take a look at this example.

    from openerp import models, fields, api, exceptions
    
    class YourReport(models.AbstractModel):
        _name = 'report.your_addon.report_template_id'
    
        @api.multi
        def render_html(self, data=None):
            report_obj = self.env['report']
            report = report_obj._get_report_from_name('your_addon.report_template_id')
            model1_docs = self.env['your_addon.your_model1'].search([('something','=','something')])
            model2_docs = self.env['your_addon.your_model2'].search([('something','=','something')])   
            docargs = {
                'doc_model': report.model,
                'model1_docs': model1_docs,
                'model2_docs': model2_docs,
            }
            return report_obj.render('your_addon.report_template_id', docargs)
    

    UPDATE WITH MODIFIED CONTEXT:

    To call your report with a modified context try the following (untested). I could not find any examples of calling reports with modified context however did not look extensively.

    ctx = self.env.context.copy()
    ctx.update({'domain':[('something','=','something')]})
    self.with_context(ctx)
    return {
        'name':'Report',
        'type':'ir.actions.report.xml,
        'report_name':'your_addon.report_template_id',
        'report_type':'qweb-pdf'
    }
    

    Then from within your report function we defined earlier you should be able to access context through your environment.

    domain = self.env.context.get('domain')
    

    You will need to create a function in your wizard which calls the report passing the context.

提交回复
热议问题