1

im using odoo13, and want try to make download excel file, heres my models odoo :

class CustomExcel(models.TransientModel):
    _name = 'custom.excel'
    _rec_name = 'datas_fname'

    file_name = fields.Binary(string="Report", readonly=True)
    datas_fname = fields.Char(string="Filename")


class MailingContact(models.Model):
    _inherit = 'mailing.contact'

    def action_download(self):
        filename = "template.xlsx"
        workbook = xlwt.Workbook(encoding='utf-8')
        sheet1 = workbook.add_sheet('saff', cell_overwrite_ok=True)
        format1 = xlwt.Style.easyxf('font: color black, bold True;')

        sheet1.col(0).width = 7000
        sheet1.write(0,0,"column 1", format1)

        stream = BytesIO()
        workbook.save(stream)
        out = base64.encodebytes(stream.getvalue())

        excel_id = self.env['custom.excel'].create({"datas_fname":filename,
                                                    "file_name": out
                                                    })

        return {
            'res_id': excel_id.id,
            'name': 'Template Download',
            'view_mode': 'form',
            'res_model': 'custom.excel',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }

When I run the action_download() function, the downloaded file has the name "custom.excel-<id>-file_name" without the ".xlsx" extension. I want to rename the downloaded file to "template.xlsx".

How can I rename the downloaded file to "template.xlsx" in this Odoo model?

Kenly
  • 24,317
  • 7
  • 44
  • 60
perl
  • 15
  • 4

1 Answers1

0

Try to set the filename attribute on the binary field

Example:

<record id="custom_excel_view_form" model="ir.ui.view">
<field name="name">custom.excel.view.form</field>
<field name="model">custom.excel</field>
<field name="arch" type="xml">
    <form>
        <group>
            <field name="file_name"  filename="datas_fname"/>
            <field name="datas_fname" invisible="1"/>
        </group>
    </form>
</field>
</record>
Kenly
  • 24,317
  • 7
  • 44
  • 60