problem description
I intend to customize the excel download in odoo, but I can only add the "Export excle" option successfully under the form attempt. The tree view does not have this option
form view
tree
Code:
view.xml
<act_window id="action_export_wizard"
name="Excel"
src_model="split.accountant.total"
res_model="export.wizard"
view_type="tree" view_mode="form"
target="new"/>
<record id="wizard_view" model="ir.ui.view">
<field name="name">export wizard</field>
<field name="model">export.wizard</field>
<field name="arch" type="xml">
<form string="">
<footer>
<button string="" class="btn-primary" name="action_export" type="object"/>
<button string="" class="btn-default" special="cancel" />
</footer>
</form>
</field>
</record>
model.py
class ExportWizard(models.Model):
_name = "export.wizard"
file = fields.Binary("")
def generate_excel(self, product_ids):
"""
excel
"""
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet("")
-sharp add header
header = ["", "", ""]
for col in range(len(header)):
worksheet.write(0, col, header[col])
-sharp add data
for row in range(1, len(product_ids)+1):
product_id = product_ids[row-1]
worksheet.write(row, 0, product_id.company_name)
worksheet.write(row, 1, product_id.accountant_num)
worksheet.write(row, 2, product_id.accountant_total)
-sharp save
buffer = BytesIO()
workbook.save(buffer)
return base64.encodebytes(buffer.getvalue())
@api.multi
def action_export(self):
context = dict(self._context or {})
print("context=",context)
active_ids = context.get("active_ids", []) or []
print("active_ids=", active_ids)
product_tmpl_ids = self.env["split.accountant.total"].search([("id", "in", active_ids)])
self.file = self.generate_excel(product_tmpl_ids)
value = dict(
type="ir.actions.act_url",
target="new",
url="/web/content?model=%s&id=%s&field=file&download=true&filename=accountant.xls" % (self._name, self.id),
)
return value