Skip to Content
Menu
This question has been flagged
1 Reply
4891 Views

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!

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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.

https://www.odoo.com/forum/help-1/question/read-file-contents-on-module-location-156533#answer-156535

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

Author

Thank you so much for your help.

Related Posts Replies Views Activity
3
Nov 22
4041
1
Apr 19
7044
2
Dec 23
12010
1
Dec 23
437
0
Oct 23
33