生成可下载的Odoo CSV导出
我遇到了一个问题,想生成一个可以下载的CSV文件,大家知道怎么解决这个问题吗?这是我的临时模型
class ReportPatientWizard(models.TransientModel):
_name = "report.patient.wizard"
_description = "Patient Reports"
patient_id = fields.Many2one('hospital.patient', string='Patient', readonly=True)
gender_filter = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')],
string='Gender Filter')
def _prepare_csv_data(self, patients):
field_names = ['Name', 'Age', 'Gender'] # Field names for CSV headers
data = io.StringIO()
writer = csv.DictWriter(data, fieldnames=field_names)
writer.writeheader() # Write CSV header
for patient in patients:
row = {
'Name': patient.name,
'Age': patient.age,
'Gender': patient.gender,
# Add other fields as needed
}
writer.writerow(row)
return data.getvalue()
@api.multi
def download_patient_report(self):
# Ambil data pasien berdasarkan ID yang dipilih
patient = self.patient_id
# Filter data berdasarkan gender jika gender_filter dipilih
if self.gender_filter:
patients = self.env['hospital.patient'].search([('gender', '=', self.gender_filter)])
else:
patients = self.env['hospital.patient'].search([])
if not patients:
raise UserError(_('No patients found matching the criteria.'))
csv_data = self._prepare_csv_data(patients)
print(f"Records: {patients}")
# Prepare file name
filename = f'patient_report_{fields.Date.today()}.csv'
# Return a response to download the CSV file
return {
'type': 'ir.actions.act_url',
'url': 'web/content/?model=report.patient.wizard&id={}&filename={}&field=file'.format(self.id, filename),
'target': 'current',
}
def print_patient_report(self):
# Generate and download patient report as CSV
return self.download_patient_report()
这是view.xml文件
<record id="report_patient_view" model="ir.ui.view">
<field name="name">Report Patient</field>
<field name="model">report.patient.wizard</field>
<field name="arch" type="xml">
<form string="Report Patient">
<!-- <field name="patient_id"/>-->
<group col="1">
<!-- <field name="is_gender"/>-->
<field name="gender_filter"/>
</group>
<!-- <field name="journal_ids" required="0" invisible="1"/>-->
<footer>
<button name="download_patient_report" string="Download" type="object" class="oe_highlight"/>
<button string="Cancel" class="btn btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_report_patient_view" model="ir.actions.act_window">
<field name="name">Report Patient</field>
<field name="res_model">report.patient.wizard</field>
<field name="type">ir.actions.act_window</field>
<field name="view_mode">form</field>
<field name="view_id" ref="report_patient_view"/>
<field name="target">new</field>
</record>
<menuitem id="menu_report"
name="Report Patients"
parent="menu_hospital_operations"
action="action_report_patient_view"
sequence="15"/>
在这里,我想做一个表单过滤器,让用户可以选择他们想要的数据性别。但是当我点击下载按钮时,出现了问题,这是错误信息
我本来期待的是,点击下载按钮后就能生成下载文件。
1 个回答
1
你在路由中指定了一个字段,但你并没有一个叫做 file
的字段。你需要添加一个二进制字段,并在文件名后面设置它的内容。
举个例子:
# Prepare file name
filename = f'patient_report_{fields.Date.today()}.csv'
self.file = base64.b64encode(csv_data.encode('utf-8'))
你可以在 download_patient_report
函数的开头加上 self.ensure_one()
,这样可以确保 self
只包含一个记录。