title: how to populate django model with a foreign key from a csv
body: <p>my model.py:</p>
  
  <pre><code># Riders models ----------------------------------------
  class CategoryTeam(models.Model):
  
      name = models.CharField(max_length = 256)
  
      def __str__(self):
          return self.name
  
  class Teams(models.Model):
  
      name = models.CharField(max_length = 256)
      code = models.CharField(max_length = 10)
      nation = models.CharField(max_length = 10)
      continent = models.CharField(max_length = 10)
      category = models.ForeignKey(CategoryTeam, on_delete=models.CASCADE,)
  
      def __str__(self):
          return self.name
  
  #-----------------------------------------------------#
  </code></pre>
  
  <p>my script to populate</p>
  
  <pre><code>from basic_app.models import CategoryTeam,Teams
  
  
  
  def populateCat():
      f = open('CategoryCSV.csv')
      reader = csv.reader(f)
      next(reader)
      for row in reader:
  
          # Create new User Entry
          category = CategoryTeam.objects.get_or_create(name=row[0])[0]
  
  def populateTeamat():
      f = open('FantaDS Project - Teams.csv')
      reader = csv.reader(f)
      next(reader)
      for row in reader:
  
          # Create new User Entry
          team = Teams.objects.get_or_create(
                                          name = row[0],
                                          code = row[1],
                                          nation = row[2],
                                          continent = row[3],
                                          category = row[4]
                                          )[0]
  
  if __name__ == '__main__':
      print("Populating the databases Cat...Please Wait")
      populateCat()
      print('Populating Complete')
      print("Populating the databases Team...Please Wait")
      populateTeamat()
      print('Populating Complete')
  </code></pre>
  
  <p>the exeption generated:</p>
  
  <blockquote>
    <p>File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\utils.py",line 85, in _execute return self.cursor.execute(sql, params)<br>
    File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\utils.py", line 89, in __exit__raise ndj_exc_value.with_traceback(traceback) from exc_value<br>
    File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params)<br>
    File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\sqlite3\base.py",line 303, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: basic_app_teams.category_id</p>
  </blockquote>