Created
January 3, 2012 22:14
-
-
Save poeschko/1557228 to your computer and use it in GitHub Desktop.
Bulk insertion for Django models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BulkInsertion(object): | |
""" | |
Bulk-insert several model instances using accumulated DB statements. | |
Usage: | |
>>> with BulkInsertion('app_tablename') as inserter: | |
... inserter.insert(field1=value1, field2=value2) | |
By default, a DB statement is issued every 1000 inserts. | |
Note that foreign keys have to be specified by their DB field name, i.e. usually with an _id prefix. | |
""" | |
def __init__(self, table, n=1000): | |
self.table = table | |
self.n = n | |
def __enter__(self): | |
self.objects = [] | |
return self | |
def __exit__(self, type, value, tb): | |
self.flush() | |
def insert(self, **object): | |
self.objects.append(object) | |
if len(self.objects) >= self.n: | |
self.flush() | |
def flush(self): | |
if self.objects: | |
keys = self.objects[0].keys() | |
keys.sort() | |
values = [] | |
for object in self.objects: | |
values.extend([object[key] for key in keys]) | |
cursor = connection.cursor() | |
values_placeholder = ','.join(['(%s)' % ','.join(['%s'] * len(keys))] * len(self.objects)) | |
sql = "INSERT INTO %s (%s) VALUES %s" % (self.table, ",".join(keys), values_placeholder) | |
cursor.execute(sql, values) | |
self.objects = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment