Home > other >  Excel Data for Looping Using Python Pandas before Django Model Insert
Excel Data for Looping Using Python Pandas before Django Model Insert

Time:09-05

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

enter image description here

django models

enter image description here

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.

  • Related