0

Right now, I have a Django application with an import feature which accepts a .zip file, reads out the csv files and formats them to JSON and then inserts them into the database. The JSON file with all the data is put into temp_dir and is called data.json.

Unfortunatly, the insertion is done like so:

Building.objects.all().delete()
call_command('loaddata', os.path.join(temp_dir, 'data.json'))

My problem is that all the data is deleted then re-added. I need to instead find a way to update and add data and not delete the data.

I've been looking at other Django commands but I can't seem to find out that would allow me to insert the data and update/add records. I'm hoping that there is a easy way to do this without modifying a whole lot.

Mike
  • 2,514
  • 2
  • 23
  • 37
  • maybe something like this https://gist.github.com/datamafia/9671827 – Joran Beasley May 23 '14 at 16:44
  • 1
    How do you determine when you should update a record vs add it? – schillingt May 23 '14 at 16:45
  • @schillingt In our csv files, there are two fields that would be updated (IP and Port) and other fields that will never be updated (Building Number, Building Name, Floor, Room, etc.). We would update if a new records Building number, etc. were already in the database but the IP and port were different. We would add if the Building number, etc. did not exist. Very good question – Mike May 23 '14 at 16:59
  • What happens if you set the pk to null like this answer? http://stackoverflow.com/a/1505083/1637351 – schillingt May 23 '14 at 17:19

1 Answers1

1

If you loop through your data you could use get_or_create(), this will return the object if it exist and create it if it doesn't:

obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)})
Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29
  • This looks like what I need, I wont have a chance to test it until Tuesday but thank you for the swift reply. – Mike May 23 '14 at 17:48
  • btw, I have a follow up to this question here: http://stackoverflow.com/questions/23897349/how-to-use-django-model-functions – Mike May 27 '14 at 19:23