Excel Data for Looping Using Python Pandas before Django Model Insert

Question:

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

Asked By: harmain

||

Answers:

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.

Answered By: kochul
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.