title: import data into sqlite database with foreign key django
body: <p>I want to import student's name list to database with one foreign key which is the class infomation. </p>
  
  <p>Namelist Model.py:</p>
  
  <pre><code>name = models.CharField(max_length=100)
  program = models.CharField(max_length=10)
  classGrp = models.ForeignKey('GroupInfo', on_delete=models.SET_NULL, null=True)
  </code></pre>
  
  <p>the data in the namelist is like this:</p>
  
  <pre><code>id      name            program     classGrp    
  
  1   ABHISHEK SINGH  CE1 1
  </code></pre>
  
  <p>GroupInfo Model.py:</p>
  
  <pre><code>class GroupInfo(models.Model):
      classGrp = models.CharField(max_length=10)
      day = models.CharField(max_length=15)
  </code></pre>
  
  <p>The data in the groupInfo is like this: </p>
  
  <pre><code>id      classGrp   day
  
  1   FEP1    Tues    
  
  2   FSP1    Wed 
  </code></pre>
  
  <p>When i import the namelist data, eg as below:</p>
  
  <pre><code>id  name    program     classGrp
  
  137 Dummy   FP      2
  </code></pre>
  
  <p>It does not store 2, which is the classGrp. When i check the detail of the classGrp at <a href="http://127.0.0.1:8000/admin" rel="nofollow noreferrer">http://127.0.0.1:8000/admin</a>: The classGrp is in dropdownlist displaying the classgr ( FEP1,FSP1) instead of the ID (1,2). </p>
  
  <p>VIEW.PY:</p>
  
  <pre><code>def import_studName(request):
      template = "import_stud.html"
  
      prompt = {
          'order' : 'Kindly ensure that the order of CSV should be id, name, program, classGrp'
      }
  
      if request.method == "GET":
          return render(request, template, prompt)
      csv_file = request.FILES['file']
  
      if not csv_file.name.endswith('.csv'):
          messages.error(request, 'This is not a csv file')
  
      data_set = csv_file.read().decode('UTF-8')
      io_string = io.StringIO(data_set)
  
      next(io_string) 
      for column in 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 = column[3]
          )
  
  
      context = {}    
      return render(request,template, context)
  </code></pre>
  
  <p>And that is the reason why it does not import the classGrp data which is 2. </p>
  
  <p>How do i import a foreign key id to the database?</p>
  
  <p>ERROR MESSAGE:</p>
  
  <p>Cannot assign "'2'": "Namelist.classGrp" must be a "GroupInfo" instance.</p>