title: How do you import a CSV file into django with foriegn keys
body: <p>I'm trying to import data from a csv file into Django but I keep getting this error :</p>
  <blockquote>
  <p>NOT NULL constraint failed: error_boilermodel.ModelBrand_id</p>
  </blockquote>
  <p>my models are this :</p>
  <pre><code>        from django.db import models
      
      # Create your models here.
      class BoilerBrand(models.Model):
          BrandName = models.CharField(max_length=40, blank=False)
      
          def __str__(self):
              return self.BrandName
      
          class meta:
              db_table = 'Boiler_Brand'
      
      
      class BoilerModel(models.Model):
          ModelBrand = models.ForeignKey('BoilerBrand', on_delete=models.CASCADE,)
          ModelName = models.CharField(max_length=600, blank=False)
      
          def __str__(self):
              return self.ModelName
          class meta:
              db_table = 'Boiler_Model'
      
      class BoilerError(models.Model):
          BoilerBrand = models.ForeignKey('BoilerBrand', on_delete=models.CASCADE,)
          BoilerModel = models.ForeignKey('BoilerModel', on_delete=models.CASCADE,)
          ErrorCode = models.CharField(max_length=40, blank=False)
          ErrorDesc = models.CharField(max_length=200, blank=False)
          ErrorFix = models.CharField(max_length=200, blank=False, default=&quot;Call a technician&quot;)
      
      
          def __str__(self):
              return self.ErrorCode
      
          class meta:
              db_table = 'Boiler_Error'
  </code></pre>
  <p>My import code is this :</p>
  <pre><code>    import csv
  from error.models import BoilerError,BoilerBrand,BoilerModel
  ImportBrandName = &quot;worcester-bosch&quot;
  
  def run():
      file = open(&quot;D:/python tratining/boilererror/error/csv-error-codes/worcester-bosch-error-codes-Copy.csv&quot;)
      reader = csv.reader(file)
      BoilerError.objects.all().delete()
      BoilerModel.objects.all().delete()
      BoilerBrand.objects.all().delete()
      for row in reader:
          print(row)
          a, created = BoilerBrand.objects.get_or_create(BrandName=ImportBrandName)
          b, created = BoilerModel.objects.get_or_create(ModelName=row[2])
          c, created = BoilerModel.objects.get_or_create(ModelBrand=ImportBrandName)
          d, created = BoilerError.objects.get_or_create(BoilerBrand=ImportBrandName)
          e, created = BoilerError.objects.get_or_create(ErrorCode=row[0])
          f, created = BoilerError.objects.get_or_create(ErrorDesc=row[1])
          g, created = BoilerError.objects.get_or_create(ModelName=&quot;Contact a Technician&quot;)
  </code></pre>
  <p>here is a sample of the CSV I'm using:</p>
  <p>200 O ,Boiler in heating mode , Greenstar 8000 Boiler</p>
  <p>201 O ,Boiler in hot water mode , Greenstar 8000 Boiler</p>