1

I want to write a Python script to import the contents of CSV file into a Django app's database. So for each CSV record, I create an instance of my model, set the appropriate values from the parsed CSV line and call save on the model instance. For example, see below:

for row in dataReader:
    person=Person()
    person.name=row[0]
    person.age=row[1]
    person.save()

Now, lets say that the name Field is marked as unique in the Model. What's the best way to handle the situation where the record being imported has the same Name value as one already in the database? Should I check for this before calling save? How? Should I catch an exception? What would the code look like?

EDIT: If a record already exists in the db with the same name field, I would still like to update the other fields. For example, if I was importing Fred,43 and there was already a record Fred,42 in the db it should update the db to Fred,43.

EDIT: Thanks for all the answers. This approach, pointed to by chefsmart, is the one I think I will go with:

try:
    obj = Person.objects.get(name=name)
except Person.DoesNotExist:
    obj = Person()
    obj.name = name
obj.age = age
obj.save()
FunLovinCoder
  • 7,597
  • 11
  • 46
  • 57
  • A naive solution would be to get a list of names beforehand and check: if name in name_list: do the save(). I dunno if this is the best though. – Uku Loskit Nov 03 '10 at 11:47

3 Answers3

5

One of the Django orm function that i love so much is get_or_create()

so i will suggest you do like this:

for row in dataReader:
    person_record, created = person.get_or_create(name=row[0], age=row[1])

you can check after if you want to change the old record in person_record or check if the record was created if created: and do what ever you want with it ..

hope this will help

mouad
  • 67,571
  • 18
  • 114
  • 106
  • If the record already exits, but some of the non unique Fields are different does it do an update? Do I have to call a method such as person_record.save()? – FunLovinCoder Nov 03 '10 at 13:47
  • no it don't do an update is like the name of the method say ; __get if exist or create a new one__ if you want to update the record __get_or_create__ return the record if it exist in my answer it's the var __person_record__ so you can change it and save() – mouad Nov 03 '10 at 14:12
1

Something like that:

for row in dataReader:
    try:
       Person.objects.get(name=row[0])
       #write some errlog here possibly or update the model
    except Person.DoesNotExist:
       Person.object.create(name=row[0],age=row[1])

It will possibly be better to know you stumbled into duplicate or not. Also you don't depend on whether the model was written correctly or database supports unique keys etc.

Frost.baka
  • 7,851
  • 2
  • 22
  • 18
0

Catch the django.db.IntegrityError, I reckon

Steve Jalim
  • 11,989
  • 1
  • 37
  • 54
  • I tried that, but after catching the exception it stops with: django.db.utils.DatabaseError: current transaction is aborted, commands ignored until end of transaction block – FunLovinCoder Nov 03 '10 at 12:06
  • What do you do when you catch the IntegrityError? Throw away the model you just created which contains the duplicate, yep? If so, maybe you do have to do a get_or_create – Steve Jalim Nov 03 '10 at 13:30