Skip to content Skip to sidebar Skip to footer

Import Data Into Sqlite Database With Foreign Key Django

I want to import student's name list to database with one foreign key which is the class infomation. Namelist Model.py: name = models.CharField(max_length=100) program = models.Ch

Solution 1:

Managed to solve my own question. My mistake was that i didnt call classgrp in the namelist.update_or_create.

Here's the updated answer:

forcolumnin csv.reader(io_string, delimiter=',', quotechar="|"):

    if column[3] !='':
            classGrp = GroupInfo.objects.get(pk = (column[3]))
    else: 
        classGrp = GroupInfo.objects.get()
    _, created = Namelist.objects.update_or_create(
        id=column[0],
        name=column[1],
        program=column[2],
        classGrp = GroupInfo.objects.get(pk = (column[3]))

            )

Post a Comment for "Import Data Into Sqlite Database With Foreign Key Django"