Hi All I am trying to import a data from an excel sheet to the models in Django.
I have a little snippet to parse the data in the excel sheet and a 'mapping' that maps my models to the excel header names.
keys = [sheets.cell(0, col_index).value for col_index in xrange(sheets.ncols)]
dict_list = []
for row_index in xrange(1,sheets.nrows):
d_list = {
keys[col_index]:sheets.cell(row_index,col_index).value
for col_index in xrange(sheets.ncols)
}
print d_list
dict_list.append(d_list)
print dict_list
for every_row in dict_list:
print every_row['name']
mapping={'address.name' :['NAME', 'name',],
'address.street':['street','way'],
'study.description':['education','degree'],
.....'
}
Now I have to build a dictionary to map column headers/numbers from the excel sheet to the models/fields with the mappings dictionary and first line of the given excel sheet. how should I incorporate the mappings dict in my snippet of parsing the excel. Any help would be much appreciated. PS. I Am new to python and django so if you see any absurd terms/ coding practice please correct me. Thanks
I need to achieve something like this :
dict_mapping={0:'address.name',
1:'address.street',
2:'some models.field',
3:'model.field',
}
Try to build your dictionary in a way that maps your fieldnames to the values you extract. Then use something like models.objects.create(**mydict_mapping)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.