4

I have spent quite sometime to figure this out. I am simply trying to import a CSV file using Python's csv module and Django's get_or_create().

This is my simple code (built upon this code):

import csv
from .models import Person

def import_data():
    with open('/path/to/csv/people_list.csv') as f:
           reader = csv.reader(f)
           for row in reader:
               _, created = Person.objects.get_or_create(
                   name=row[0],
                   p_id=row[1],
                   current_status=row[2],
                   )

I get the following error when I run import_data() on the shell

peoplelisting.models.DoesNotExist: Person matching query does not exist.

Yes, this particular Person does not exist but isnt that the whole point of using get_or_create()? If it doesnt exist, create it?

Anupam
  • 14,950
  • 19
  • 67
  • 94

2 Answers2

3

After a lot of playing around, finally figured the issue was the following:

My csv also contained a header row which I was not ignoring. I had thought I'll proceed piece-meal and would ignore the header only after I get the csv-importing to work but the header itself was creating the problem (thanks to this post which (indirectly) helped a lot). The values in the header did not match the schema (max_length etc.) and thats what Person matching query does not exist was referring to. Ignoring the header made it work. I just hope that the error message was more descriptive though. Hope it helps someone else save the hours I spent debugging a simple thing. Here's the correct code:

import csv
from .models import Person

def import_data():
    with open('/path/to/csv/people_list.csv') as f:
           reader = csv.reader(f)
           for row in reader:
              if row[0] != 'Person_name': #where Person_name is first column's name
                 _, created = Org.objects.get_or_create(
                     name=row[0],
                     p_id=row[1],
                     current_status=row[2],
                     )
Anupam
  • 14,950
  • 19
  • 67
  • 94
2

Instead of having to check row[0] every time, you could just skip the first row:

next(reader, None)  # skip the headers

source: Skip the headers when editing a csv file using Python