a wizard method to download some information of the sale order - order lines to csv file
this method have features as :
- get current sale order
- get product id and product quantities from sale order lines
- write those into a csv file
- initiate the download of csv file
- auto close the wizard
import base64
import csv
from io import StringIO
def action_download_template(self):
sale_order = self.get_current_sale_order()
dynamic_filename = f"so_{sale_order.id}_template.csv"
partner_id = sale_order.partner_id.id if sale_order.partner_id else ''
data = [["Partner ID", "Product ID", "Product Quantity"]]
for line in sale_order.order_line:
data.append([partner_id, line.product_template_id.default_code, line.product_uom_qty])
csv_data = StringIO()
writer = csv.writer(csv_data)
writer.writerows(data)
csv_data.seek(0)
csv_base64 = base64.b64encode(csv_data.getvalue().encode('utf-8'))
csv_data.close()
self.write({'csv_file': csv_base64, 'file_name': dynamic_filename})
return {
'type': 'ir.actions.act_url',
'url': f"/web/content/?model=sale.order.import.wizard&id={self.id}&field=csv_file&download=true&filename={dynamic_filename}",
'target': 'self',
'next': {'type': 'ir.actions.act_window_close'},
}
If the above is not working and if you have a method to create a csv file as server action then - try below
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<!-- Server Action for ASN Manifest -->
<record id="action_unreserve_picking" model="ir.actions.server">
<field name="name">Create ASN Manifest</field>
<field name="model_id" ref="stock.model_stock_picking"/>
<field name="binding_model_id" ref="stock.model_stock_picking"/>
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">
if records:
action = records.action_create_asn_manifest()
</field>
</record>
</odoo>
Problem:
When running the action directly in Odoo, the system did not interpret the return URL correctly, leading to no file download trigger.
Some Odoo UI actions expect an explicit return action to be handled properly.
The previous ir.actions.act_url was not being executed in the correct context.
The Fix: Using a Server Action Correctly
Your XML fix works because it properly triggers the return action inside an Odoo server action.
What this does:
Calls action_create_asn_manifest() inside the Odoo action execution pipeline.
Ensures that Odoo properly interprets the return dictionary (ir.actions.act_url).
Forces Odoo to trigger the browser download behavior.
Top comments (0)