Django Quick Tip – Bulk Inserts

Feb 19, 2014
Django
dj-logo

An Intro to Django Quick Tips: This post starts a new series where I will give fast, easy tips on some of the lesser known or sometimes just plainly overlooked aspects of Django.

Django, in case you are unaware, is an incredible, elegant Python web framework that allows for extremely fast development. In my opinion, it’s the best web framework out there and the fact that I can code in Python just makes happy.

Django is also a very active open-source project, with great new features and enhancements with each release. This, along with the framework being fairly large to begin with, makes it easy to miss cool features and techniques as they come out….like this one:

bulk_create

When I started using Django in version 1.1, there was no built-in way to handle bulk inserts or updates. I came across a need for this when I was writing a system that processed batches with thousands of payments and cash outs at a time. Initially I just simply looped through the payments and let the ORM handle saving each one. This took an enormous amount of time was very ineffecient. I was able to find a snippet on DjangoSnippets that turned handled bulk updating simply and easily and turned a process that took, at times, 10 minutes to one that took less than 1 minute.

Well, since Django 1.4 we now have a built in method for doing bulk inserts, and it goes a little something like this:

bulk_create takes in a list of model objects that are ready to be saved, so…

 

Comments
  • What about bulk saves? Is there a better solution rather than iterating the set of objects and saving one by one?

    Georgek 10/20/2016 8:31 am Reply
    • You can just update an entire queryset with update(). So, for example, if you wanted to change all records that have the name Tim to Timothy in an Employee model, you could do:

      Employee.objects.filter(name="Tim").update(name="Timothy")

      Check this out for more info: https://docs.djangoproject.com/en/1.9/ref/models/querysets/#update

      codyparker 10/25/2016 2:02 pm Reply

Leave a Comment

Your email address will not be published. Required fields are marked *