Enable Dark Mode!
how-to-handle-csv-file-operations-in-odoo-17.jpg
By: Rahna Rasheed

How to Handle CSV File Operations in Odoo 17

We need to create various file types in Odoo for various uses. Different systems rely on CSV or XML files for manual integration or similar operations because it is convenient to obtain data from them. For instance, the data is organized in a particular way in CSV files. Data can be separated using commas, colons, semi-colons, and so forth. and the subsequent row is regarded as the following line. Now we can import the ‘CSV’ package to perform CSV file operations.

import csv

Importing data into Odoo from CSV files allows you to add new records, update existing records, or perform bulk operations efficiently. Odoo provides a user-friendly import interface to map CSV columns to Odoo model fields and handle various import scenarios. To import data.

1. Navigate to the desired model's list view.

2. Click on the "Import" button and choose the CSV file to upload. Follow the import wizard to map CSV columns to corresponding model fields, configure import options, and validate data. After validation, proceed with the import process, and Odoo will create or update records based on the CSV data.

In this example, we will write record data to a CSV file. Take the case where we export product records as an example. We must first create a CSV file in the instance that follows, export. The file name is csv. The mode for writing a file must be 'w'. Next, we must use the writer function to write data into the file and use the writer row() function to iterate the records across the lines.

  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])
       data = output.getvalue().encode('utf-8')
   return data

delimiter: which defines the separator.

quote char: which defines the data enclosed with.

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.

def export_customers(self):
   import importlib
   importlib.reload(base64)
   data = self.env['export.csv'].export_product()
   self.csv_data = base64.b64encode(data).decode('utf-8')
   self.filename = 'export.csv'
   return {
       'type': 'ir.actions.act_url',
       'url': "web/content/?model=wiazrd.export.csv&id=" + str(self.id) +
        "&filename=export.csv&field=csv_data&download=true&filename=" + self.filename,
       'target': 'self',
   }

In the above code, import lib is used to import modules dynamically. In most cases, base64 is already available in the standard library, and reloading it might not be required. In this data, it fetches all the details of products, then decode data into a string and sets the desired filename for downloading the CSV file. This blog helps to manage data exchange through CSV files in Odoo 17.

To read more about How to Generate CSV Reports in Odoo 16, refer to our blog How to Generate CSV Reports in Odoo 16


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