This question has been flagged
1 Reply
3894 Views

So, I am having the code to update my field task_name from similar excel field.

 

But Things are not going to happen.. Please give a loot at the vode below

   excel_file= fields.Char('/home/mazhar/Desktop/Odoo project activity list.xlsx',size = 250),
    sheet_name =fields.Char('Activities Sheet'),
    db_name = fields.Char('spectec'),

 

 def action_list(self,cr,uid,ids,context=None):
    
        #wbk = xlwt.Workbook()
        #sheet1 = wbk.add_sheet('my sheet')  

        #file_data=StringIO.StringIO()
        #o=wbk.save(file_data)    
        #out=base64.encodestring(file_data.getvalue())
        #return out

        for data2 in self.browse(cr, uid, ids, context=context):
            file_path = data2.excel_file
            sheet_name_1 = data2.sheet_name
            db_name_1 = data2.db_name
       # connection for DB
            conn_string = "dbname='"+ db_name_1+"' user='admin' password='admin' host='localhost'"
            conn = psycopg2.connect(conn_string)
            cursor = conn.cursor()
        # read the excel file
            wb_c = openpyxl.reader.excel.load_workbook(file_path)
            s_c  = wb_c.get_sheet_by_name(name=sheet_name_1)
            d_c = s_c.calculate_dimension()
            r_c = s_c.range(d_c)
        for row in r_c:
            task_name_from_excel =row[0].value
            statement = "select task_name from activity_track where LOWER(name)=LOWER('"+task_name_from_excel+"')"
            cursor.execute(statement)
            conn.commit()
            task_name = cursor.fetchone()
            

Pleae need your sincere guidance

 

Thanks

Avatar
Discard
Author

Thanks..But action_list is my button name which will be clicked and update the field

method name : action_list is ok... and button is ok.. no prob with it.. read the rest of comments ;)

Author

You mean I should use something like this cr.execute("SELECT anyvalues FROM anytable WHERE id =%s",ids) result= map(lambda x: x[0], cr.fetchall())

Author

Also please answer my other question for excel also plzz.. In Odoo forum..About showing table on front end Thanks

jup.. you got the point of cr.execute... other question.. please provide link to it so i don't need to search..

Author

Here is the Link https://www.odoo.com/forum/help-1/question/buttton-click-event-73086 I have an excel file and that file has some values. I have a condition like if I select some check box then 2nd value from database will be shown only on button click and then save as excel will take place (one more button will be there in wizard view I am getting). So, what you suggest.. I should upload excel file fom some control in odoo ? Or I shoupd put all values from excel to some table?

Best Answer

Here is some comments... 
1. fields declaration is wrong:
excel_file= fields.Char('/home/mazhar/Desktop/Odoo project activity list.xlsx',size = 250), -> dont put actual value in field name... 
should be: 
excell_file = fields.Char('Path to file')  # size is not needed... values will contain actual path to file... 
hint: why not using field binary or attachment insted so you can upload file when needed ( put it in transient model if no need to keep old xls files..)

in action list.. 
the part with db connection string is also apsolutly not needed.. except if you try to connect to some other db.. 
this line : def action_list(self,cr,uid,ids,context=None):
states that you want db cursor ( or  cr  in odoo) to be passed to method... so why on earth you need to initiate it again?
yust use cr.execute(some_sql) 

try this , look what happens.. .

hope it helps

Avatar
Discard