- Create a Python module (e.g., my_module) in your Odoo add-ons folder.
- Inside the module, create a Python file (e.g., models.py) and define a model to store the data from the CSV file. For example:
- from odoo import models, fields
- classMyModel(models.Model):
- _name = 'my_module.my_model'
- name = fields.Char(string='Name')
- email = fields.Char(string='Email')
# Add more fields as needed
Create a CSV file (e.g., data.csv) with the corresponding headers and data you want to import. Place it in the module's folder.
In the same Python file (models.py), add a method to read the CSV file and create records in the model. For example:
import csv
from odoo import models, api
classMyModel(models.Model):
# ...
@api.modeldefimport_data_from_csv(self):
with open('my_module/data.csv', 'r') as file:
csv_data = csv.DictReader(file)
for row in csv_data:
self.create({
'name': row['Name'],
'email': row['Email'],
# Set values for other fields
})