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:
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…
#a list of games that need to be created
game_list = ['Puerto Rico', 'Eclipse', 'Settlers of Catan', 'Agricola']
#the list that will hold the bulk insert
bulk_games = 
#loop that list and make those game objects
for g in game_list:
new_game = Game()
new_game.title = g
#add game to the bulk list
#now with a list of game objects that want to be created, run bulk_create on the chosen model