In this tutorial I will show you how to save or insert data into the database table using odoo "copy_from" function. Odoo provide this "copy_from" function in "self.env.cr" (current database courser).
What does self.env.cr.copy_from do?
The copy_from function takes almost four parameters.
- CSV File: Takes content that need to be stored into the database table
- Table Name: Name of the table, where to stores the CSV content
- Separator: Symbol that distinguish between columns e.g. (|,;,etc)
- Column: Column of the table in which CSV data to be inserted
What we are going to do?
We are going to save or insert CSV data into the table using "self.env.cr.copy_from". To do this we need to do following activity.
- Create table using "self.env.cr.execute"
- Open CSV file
- Create list of column (fourth parameter of "copy_from" function)
- Copy CSV data into table using "self.env.cr.copy_from"
- Close CSV file