Hi My friend,
i want to create a function that when user click the button will popup a form that let the user click the url to download the excel file. following is my code , it can create the excel file and write to the binary field, but the popup form doesn't show up . what is wrong with my code?
Thank you !
following is my code:
1. define the button
<button name="export_excel" type="object" string="Create Reports"/>
2. define view
<record id="excel_download_form" model="ir.ui.view">
<field name="name">Export Excel</field>
<field name="model">excel.report</field>
<field name="arch" type="xml">
<form create="0" string="Excel Report">
<group>
<field name="file_name" invisible="1"/>
<field name="excel_file" readonly="1" filename="file_name" nolabel="1"/>
</group>
<footer>
<button special="cancel" string="Cancel" type="object" class="btn-default"/>
</footer>
</form>
</field>
</record>
<record id="excel_form_view" model="ir.actions.act_window">
<field name="name">Export Report</field>
<field name="view_id" ref="excel_download_form"/>
<field name="type">ir.actions.act_window</field>
<field name="res_model">excel.report</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
3. python class ( the issue is when function export_excel finishing executing, return doesn't show the popup form)
class excel_report(models.TransientModel):
_name = "excel.report"
excel_file = fields.Binary('Download Excel Report')
file_name = fields.Char('File Name')
@api.multi
def export_excel(self):
filename= 'report.xls'
workbook= xlwt.Workbook(encoding="UTF-8")
worksheet= workbook.add_sheet('Excel Report')
worksheet.write(0,0,'test data')
fp = StringIO.StringIO()
workbook.save(fp)
fp.seek(0)
export_id = self.env['excel.report'].create({'excel_file': base64.encodestring(fp.getvalue()),'file_name': filename})
fp.close()
return {
'view_mode': 'form',
'res_id': export_id,
'res_model': 'excel.report',
'view_type': 'form',
'type': 'ir.actions.act_window',
'target': 'new',
}