Enable Dark Mode!
how-to-handle-csv-file-operations-in-odoo-18.jpg
By: Sruthi C Nair

How to Handle CSV File Operations in Odoo 18

Technical

Importing data into Odoo from CSV files is an efficient way to add new records, update existing ones, or perform bulk operations. Odoo offers a built-in, user-friendly interface for importing, which lets you:

1. Go to the list view of the model you want to update.

2. Click the "Import" button, upload your CSV file, and map the file's columns to Odoo fields.

3. Validate the data, resolve any issues, and proceed with importing—Odoo takes care of creating or updating the records accordingly.

In this example, we demonstrate how to export record data—specifically, product records—into a CSV file. To begin, we generate a CSV file within the Odoo environment. The file is created in write mode ('w'), allowing us to input content into it.

Using Python’s built-in csv module, we utilize the writer() function to create a CSV writer object. The header row is written first using writerow(), followed by a loop through each product record, writing its details (like product name, unit of measure, and price) line by line into the file.

This method is efficient for creating downloadable reports or sharing product data externally in a structured format.

def export_product(self):
   output = io.StringIO()
   writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
   writer.writerow(['Product Name', 'UOM', 'Price'])
   for product in self.env['product.product'].search([]):
       name = product.product_tmpl_id.name
       uom = product.product_tmpl_id.uom_id.name
       price = product.product_tmpl_id.list_price
       writer.writerow([name, uom, price])
   return output.getvalue().encode('utf-8')

This method is used to export product data to a CSV file in Odoo. It returns the encoded content of the CSV so it can be downloaded.

writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

Initializes a CSV writer object.

delimiter=',': Uses a comma as the separator between values.

quotechar='"': Wraps fields in double quotes when necessary.

quoting=csv.QUOTE_MINIMAL: Only wraps values in quotes if required (e.g., if they contain commas or special characters).

writer.writerow([name, uom, price])

* Writes the product's data as a new row in the CSV.

quoting: it has some values,

csv.QUOTE_ALL - Quote everything, regardless of type.

csv.QUOTE_MINIMAL - Quote fields with special characters.

csv.QUOTE_NONNUMERIC - Quote all fields that aren't numbers value.

csv.QUOTE_NONE – Don't quote anything in the output.

return output.getvalue().encode('utf-8')

* Gets the full string content of the CSV file and encodes it to UTF-8 bytes (required for storing it as a binary field or downloading).

def action_export_csv(self):
   data = self.export_product()
   self.file = base64.b64encode(data)
   self.filename = 'export.csv'
   return {
       'type': 'ir.actions.act_url',
       'url': "/web/content/?model=export.csv.wizard&id=" + str(self.id) +
              "&filename=" + self.filename + "&field=file&download=true",
       'target': 'self

This method is responsible for triggering the download of the CSV file that contains the product data, by using a URL action to download the file.

Call the export_product() method (which we explained earlier). This method generates the CSV content and returns it as a byte-encoded string. The data returned from export_product() is base64-encoded. This encoding is necessary because Odoo stores binary data (like files) in binary fields.

* The encoded data is stored in the file field of the export.csv.wizard model

* self.filename = 'export.csv' ,Sets the filename for the exported CSV file as export.csv. This will be the file name that the user sees when they download the file.

This is the crucial part that triggers the download action.

* The URL points to /web/content/, which is used for downloading files stored in Odoo.

* The URL contains:

a. model=export.csv.wizard: Specifies the model.

b. id= + str(self.id): Passes the current record's ID to identify the specific wizard.

c. filename= + self.filename: The filename to be used for the download.

d. field=file: Specifies the binary field that holds the file data (the file field).

e. download=true: Signals that the file should be downloaded.

* The target: 'self' ensures that the file is downloaded in the same browser window.

Handling CSV file operations in Odoo 18 is a powerful way to manage bulk data — whether you're importing large datasets or exporting custom reports. Odoo’s built-in import interface simplifies uploading data into any model, while Python's csv module allows for fully customized CSV exports directly from your backend code.

To read more about How to Handle CSV File Operations in Odoo 17, refer to our blog How to Handle CSV File Operations in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message