This question has been flagged
6 Replies
14617 Views

It seems Odoo guys have yet once more changed methods and classes so, my v10 working examples are messed up in v11...

I need to run a report by code, get the result and save it to an attachment. All by code!

Does anyone know how to do this?


SOLUTION

 #run the report and get the pdf (use the reports xml_id and pass record ids into render)

data, data_format = self.env.ref('hr_timesheet.timesheet_report').render([1,2,3])


#save the attachment

att_id = self.env['ir.attachment'].create({

            'name': 'My name',

            'type': 'binary',

            'datas': base64.encodestring(data),

            'datas_fname': 'Myname.pdf',

            'res_model': 'account.invoice',

            'res_id': invoice.id,

            'mimetype': 'application/x-pdf'

            })

Avatar
Discard

it is all over in the code, why don't you read it ?

Author

F.P. If you don't understand the basic rules of a forum meant for developers to help other developers, I suggest you try Facebook instead.

Thanks a lot for posting the solution, Rui!

Best Answer

If you are defining your report using the <report/>, see the following example:

<report id="l10n_ch_isr_report"
    model="account.invoice"
    report_type="qweb-pdf"
    string="ISR"
    name="l10n_ch.isr_report_main"
    file="l10n_ch.isr_report_main"
attachment="'ISR-' + object.number + '.pdf'"/>

 The attachment attribute will give the file a name and store it as an attachment.

You can also configure this in the UI by going to Settings -> Technical -> Reports, selecting the correct report and clicking the Advanced Properties tab. The attachment field is editable here. Note: you must be in developer mode to see these menus.

Avatar
Discard
Author Best Answer

Thanks Jake Robinson but, by "code" I mean "python". I need to be able to do this using python.


Call a report and get the attachment (or create it).

Avatar
Discard
Best Answer

Thanks Jake Robinson a lot.


Avatar
Discard