我正在尝试使用 Odoo 10 生成 excel 报告。代码运行良好,我能够做到。但是文件不会一键下载。它保存文件并在向导上显示下载链接。但我不想要这个额外的步骤。我希望通过单击下载文件。我在这里分享我的工作代码。请看一下并建议我应该添加什么以使其只需单击即可工作。
xml代码:
<div state="get">
<group>
<field name="name" colspan="4" invisible="1"/>
<field name="report" filename="name" colspan="4"/>
</group>
</div>
<button name="generate_xls_report" string="Export XLS" type="object" class="oe_highlight" />
蟒蛇代码:
from odoo import fields, models, api, _
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import xlwt
import base64
import cStringIO
from datetime import datetime
class CasesWizard(models.TransientModel):
_name = "cases.wizard"
_description = "Cases wizard"
case_id = fields.Many2one('project.project', string='Cases')
event_id = fields.Many2one('calendar.event', string='Events')
company_id = fields.Many2one('res.company', string='company id', readonly=True,default=lambda self: self.env.user.company_id.id)
lawyer_id = fields.Many2one('res.users', string='Lawyers')
#partner_id = fields.Many2one('res.partner', string='Clients')
date_from = fields.Date(string='Start Date')
date_to = fields.Date(string='End Date')
state = fields.Selection([('choose', 'choose'), ('get', 'get')],default='choose')
report = fields.Binary('Prepared file', filters='.xls', readonly=True)
name = fields.Char('File Name', size=32)
@api.multi
def generate_xls_report(self):
self.ensure_one()
wb1 = xlwt.Workbook(encoding='utf-8')
ws1 = wb1.add_sheet('Case Event Details')
fp = cStringIO.StringIO()
# Here all excel data and calculations
wb1.save(fp)
out = base64.encodestring(fp.getvalue())
self.write({'state': 'get', 'report': out, 'name':'event_details.xls'})
return {
'type': 'ir.actions.act_window',
'res_model': 'cases.wizard',
'view_mode': 'form',
'view_type': 'form',
'res_id': self.id,
'views': [(False, 'form')],
'target': 'new',
'name': 'Event Details Report'
}