I have an excel file with the following format, there is a UNIT column
, an ELEMENT column
and a SEL column
, then I want to insert the data into the database using the following Django model schema. my question is how to do the trick for looping the excel data with a structure like parent
-> child 1
-> child 2
excel data struktur
django models
CodePudding user response:
for line in sheet:
unit, element, sel = line
if unit:
unit_obj = Unit.objects.create(nama_unit=unit)
if element:
element_obj = Element.objects.create(nama_element=element, unit=unit_obj)
if sel:
SEL.objects.create(nama_sel=sel, elemen=element_obj)
I used Python's variable scope.