Skip to Content
Menu
This question has been flagged
1 Reply
3520 Views

I'm trying to export some records in csv file. 

function export_accounts() will call from button in wizard and the file download perfectly. but the wizard doesn't close automatically.

@api.multi
def export_accounts(self):
context = dict(self._context or {})
active_ids = context.get('active_ids', []) or []
data = self.env['export.csv.file'].csv_accounts(active_ids)
data = base64.encodestring(data)
attachment = {'name': 'Export Account Information',
'datas': data,
'datas_fname': 'accounts_export.csv',
'res_model': 'trans.export.csv',
'res_id': self.id}

attachment_id = self.env['ir.attachment'].create(attachment)
return {
'type': 'ir.actions.act_url',
'url': "web/content/?model=ir.attachment&id=" + str(attachment_id.id) +
"&filename=accounts_export.csv&field=datas&download=true&filename=" + attachment_id.datas_fname,
'target': 'self',
}


Avatar
Discard
Best Answer

Hi,

try to rewrite the function like below

@api.multi
def export_accounts(self):
context = dict(self._context or {})
active_ids = context.get('active_ids', []) or []
data = self.env['export.csv.file'].csv_accounts(active_ids)
data = base64.encodestring(data)
attachment = {'name': 'Export Account Information',
'datas': data,
'datas_fname': 'accounts_export.csv',
'res_model': 'trans.export.csv',
'res_id': self.id}

attachment_id = self.env['ir.attachment'].create(attachment)
return {
'type': 'ir.actions.act_url',
'url': "web/content/?model=ir.attachment&id=" + str(attachment_id.id) +
"&filename=accounts_export.csv&field=datas&download=true&filename=" + attachment_id.datas_fname,
'target': 'self',
'type': 'ir.actions.act_window_close'
}

Avatar
Discard
Author

Thanks for your reply...

if we do like this type:ir.actions.act_url doesn't work. means the wizard will be close but the file not download.

'type': 'ir.actions.act_window_close' overwrite 'type': 'ir.actions.act_url',