生成csv导出可下载odoo

问题描述 投票:0回答:1

我在生成可下载导出到 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"/>

在这里我想制作一个表单过滤器,用户可以选择他们想要获取数据的性别,但是在我单击下载按钮后它显示了问题 this is the error

我期待的是当我单击下载按钮时它会生成下载

python csv error-handling export odoo-16
1个回答
0
投票

您在路线中指定了一个字段,但没有名为

file
的字段。 您需要添加一个二进制字段并将其内容设置在文件名后面

示例:

# Prepare file name
filename = f'patient_report_{fields.Date.today()}.csv'
self.file = base64.b64encode(csv_data.encode('utf-8'))

您可以在

self.ensure_one()
函数的开头添加
download_patient_report
来验证
self
是否保存单个记录

© www.soinside.com 2019 - 2024. All rights reserved.