Hi,
I need to create a new dropdown field which will fetch data from CSV present in module, display it in dropdown of that field.
Could anyone advise me to do this.
Thanks in advance!
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
Hi,
I need to create a new dropdown field which will fetch data from CSV present in module, display it in dropdown of that field.
Could anyone advise me to do this.
Thanks in advance!
You have two option
1) take a selection field on the model and get data using model method call like
@api.model
def _get_csv_data(self):
# here read the csv file and get data
# return values as list of tuple like [('key1', 'value1')('key2', 'value2')]
my_selection = fields.Selection(selection='_get_csv_data', string='Select data')
2) take a many2one field model and on model initialization write data to a related table
@api.model_cr
def init(self):
#here read the csv file and get data
# write data to a related table using a cursor
# like self.env.cr.execute("INSERT INTO csv_data('name', ...)" values ('first' ...)
my_many2one_id - field.Many2one('csv.data')
Note: In both cases, you have to restart the server to get new data if CSV file changes dynamically after the server start. odoo load data only on start of the server
Thanks Ravi it works for me.
I have one more question where to put that csv file in odoo module. for now I have my file is on desktop and I have to give full path to open and read the file.
please help
Thanks in advance.
Note: If CSV file has any sensitive data don't put it /static directory (it serves as static assets). create a new directory in your module and put it there
Thank you so much for your help.
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
Many2one Dropdown Image/Icon
Solved
|
|
3
Nov 22
|
4041 | |
|
1
Apr 19
|
7044 | ||
Odoo Mail Sending Limit
Solved
|
|
2
Dec 23
|
12010 | |
|
1
Dec 23
|
437 | ||
|
0
Oct 23
|
33 |