Django CSV file -database upload error -


i have csv file , trying populate them sqlite database. have no error message , works fine loads last line of file.

md= md()     database = options.get('database')             filename = options.get('filename')             datareader = csv.reader(open(filename))             row in datareader:                 if row[0] != 'id':                    bb= 1 if row[3] == 'yes' else 0                    pro = 'yes' if row[4] == 'pro' else 'no'                    md.id = row[0]                    md.mol = row[1]                    md.phase = row[2]                    md.warning = black_box                    md.pro = pro                    md.status = type.objects.get(description=row[5])                    md.name = row[6]                    md.stem = row[7]                    md.year = row[8]                    md.iname = row[9]                    md.iyear = row[10]                     print row[1], row[2],row[3],row[4],row[5],row[6], row[0]                    md.save() 

but print statement prints lines in csv file. have no idea happens.

you creating 1 md instance outside of for loop, saving same instance on each iteration of loop. need create new md() instance every iteration (per line of file) if want create , save new record each line. why saving last line - over-writing pre-existing instance created. luck.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -