In Odoo, we need to generate multiple types of files for different purposes. CSV reports allow users to export structured data from an Odoo database. The CSV format is widely supported by various applications and tools, making it easy to exchange data with external systems such as spreadsheets (eg, Microsoft Excel, Google Sheets), accounting software, and other ERP systems.
CSV reports can be customized to extract specific subsets of data from the Odoo database. Users can define data extraction criteria and filters that meet their specific reporting requirements, enabling flexible and customized data extraction.
Odoo is a powerful open-source business management software that offers a wide range of features. One of the basic aspects of business data management is the generation of reports. In this blog post, we will explore how to generate CSV reports for res partners in Odoo 16 using the OCA report_csv module.
Before proceeding with the tasks at hand, it is imperative to have Odoo 16 installed and operational on your system. To ensure this, confirm the successful installation and setup of Odoo version 16. If you haven't done so already, you can refer to the official Odoo documentation for comprehensive instructions on installing Odoo 16.
Additionally, it is necessary to have the OCA report_csv module installed. This module, developed by the Odoo Community Association, introduces enhanced functionality for generating CSV reports within the Odoo platform. To install the OCA report_csv module, you can typically locate it on the official Odoo Apps website or in the OCA GitHub repository. These platforms serve as reliable sources for accessing a variety of Odoo modules developed by the community. Once installed, the report_csv module will augment your Odoo experience by providing advanced features for the seamless generation of CSV reports.
First, let's explore the process of creating a custom CSV report in Odoo 16 for the "res.partner" model. To begin, add the 'report_csv' module from OCA as a dependent module to "partner_csv_report" in the manifest.py file.
Step 1: Setting Up a Python File:
First, create a Python file called partner_csv_report.py that defines our CSV report. This file will contain the logic for which data to include in our report.
A. Creating a Python file:
Open your preferred code editor.
Create a new file and name it partner_csv_report.py.
b. Writing the logic of CSV reports:
In partner_csv_report.py you define the logic for generating the CSV report. This includes specifying the data you want to include in the report.
C. Example Python file:
# partner_csv_report.py
import csv
from odoo import models
class PartnerCSV(models.AbstractModel):
_name = 'partner.csv.report'
_inherit = 'report.report_csv.abstract'
def generate_csv_report(self, writer, data, partners):
writer.writeheader()
for obj in partners:
writer.writerow({
'name': obj.name,
'email': obj.email,
})
def csv_report_options(self):
res = super().csv_report_options()
res['fieldnames'].append('name')
res['fieldnames'].append('email')
res['delimiter'] = ';'
res['quoting'] = csv.QUOTE_ALL
return res
Step 2: Creating a report with XML
Next, we create an XML file called partner_csv_report.xml that defines how our CSV report should look like. This file helps Odoo understand where to find the assembly logic.
<!-- partner_csv_report.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<report
id="partner_csv"
model="res.partner"
string="Print to CSV"
report_type="csv"
name="report_csv.partner_csv"
file="res_partner"
attachment_use="False"
/>
</odoo>
In this XML file, you define a new report for Odoo.
* <report> is the XML tag that represents a report in Odoo. It has various attributes that provide information about the report.
* id="partner_csv": Specifies a unique identifier for the report. This ID is used to reference the report in other parts of Odoo.
* model="res.partner": Defines that this report is associated with the res.partner model. It means the report will be accessible from res partner records.
* string="Print to CSV": Specifies the user-friendly name of the report that will be displayed in the Odoo interface.
* report_type="csv": Indicates that this is a CSV report.
* name="report_csv.partner_csv": Specifies the technical name of the report, referencing the Python file where the report logic is defined.
* file="res_partner": Specifies the default name of the generated CSV file. In this case, it will be named "res_partner.csv."
* attachment_use="False": Indicates that the generated report should not be automatically attached to any records. If set to True, the report would be attached to the record it was generated from.
Step 3: Update and Install
Update your list of Odoo modules and install the new module containing the CSV report logic.
Step 4: Generate Your CSV Report
Now, you can easily create a CSV report for your res partners:
Go to your partner records in Odoo. Look for the "Print to CSV" option and use it to generate a CSV report under the print action in the form view.
Creating CSV reports in Odoo 16 is easy with the OCA report_csv module. With these simple steps, you can customize and extend your CSV reports to fit your business needs.