Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to programatically run a report and add it as an attachment? [Closed]

Julian Solorzano
on 11/13/13, 5:46 PM 5,873 views

The Question has been closed

Serpent Consulting Services Pvt. Ltd.
on 11/14/2013 02:12:00

Hello, I know I can run a report by returning an ir.actions.report.xml action from a method. Like this:

            datas = {
                'ids': context.get('active_ids',[]),
                'model': some model
                'form': data
            return {
                'type': 'ir.actions.report.xml',
                'report_name': some report
                'datas': datas,

But how do I specify that I want an attachment to be created, as well as the attachment name?

I knowI shouldn't have the need to do this in code. The report should run and attach by itself, but for some reason the "attachment" field and the "use attachment" chekbox are not working for custom reports. So, is there some way to do the attachment thing manually (for example, getting the generated binary file using the API)?



Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 11/14/13, 1:59 AM

Hello Julian Solorzano,

You may make take reference of following code.

The following code generates the report and creates the attachment.

def cover_print(self, cr, uid, ids, context=None):
    attachment_obj = self.pool.get('ir.attachment')
    for record in self.browse(cr, uid, ids, context=context):
        ir_actions_report = self.pool.get('ir.actions.report.xml')
        matching_reports = ir_actions_report.search(cr, uid, [('name','=','report_service_name_given_in_xml')])
        if matching_reports:
            report = ir_actions_report.browse(cr, uid, matching_reports[0])
            report_service = 'report.' + report.report_name
            service = netsvc.LocalService(report_service)
            (result, format) = service.create(cr, uid, [record.id], {'model': self._name}, context=context)
            eval_context = {'time': time, 'object': record}
            if not report.attachment or not eval(report.attachment, eval_context):
                # no auto-saving of report as attachment, need to do it manually
                result = base64.b64encode(result)
                file_name = re.sub(r'[^a-zA-Z0-9_-]', '_', 'Your Report Name')
                file_name += ".pdf"
                attachment_id = attachment_obj.create(cr, uid,
                        'name': file_name,
                        'datas': result,
                        'datas_fname': file_name,
                        'res_model': self._name,
                        'res_id': record.id,
                        'type': 'binary'
                    }, context=context)
    return True

Hope this will help you.


Serpent Consulting Services.

Perfect, thanks

Zenpar S.A. de C.V., Julian Solorzano
on 11/14/13, 2:13 AM

matching_reports = ir_actions_report.search(cr, uid, [('name','=','report_service_name_given_in_xml')]) --> this returning as "matching_reports::: []" can you please tell why empty list is created ?? moreover can you please tell what is report_service_name_given_in_xml ?

Sayed Anisul Hoque
on 9/18/15, 4:04 PM

report_service = 'report.' + report.report_name service = netsvc.LocalService(report_service) (result, format) = service.create(cr, uid, [record.id], {'model': self._name}, context=context) Above line was giving me error since the openerp.netsvc.LocalService() function is deprecated. This could be solved as per below editing result, format = openerp.report.render_report(cr, uid, [record.id], report.report_name, {'model': self._name}, context=context)

Sayed Anisul Hoque
on 10/15/15, 7:25 AM


| 3 2 6

Having 8+ years of OpenERP Experience. We are Specialized in OpenERP/CRM Solutions, software development, Web development, E commerce solutions & other open source solutions.

Skype : acespritech

Email : info@acespritech.com

On 11/14/13, 2:01 AM


I think Invoices report is the best example of attaching a generated report in OpenERP interface.
You can get it from here.

    attachment="(object.state in ('open','paid')) and ('INV'+(object.number or '').replace('/','')+'.pdf')"

To save report as attachment attachment and attachment_use are compulsory or you can manually add your generated report to attachment by following steps :

  1. Generate report and save in your file system.
  2. Then go to the form view of the record and click on Attachment(s) on top and add there. Below is the screenshot.
  3. You can see all your attachments which are saved in OpenERP in Knowledge -> Documents -> Documents.

image description

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 11/13/13, 5:46 PM
Seen: 5873 times
Last updated: 4/27/16, 2:13 AM