Bulk Insert Manager

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.db import models
from django.db import connection

class BulkManager(models.Manager):
        def bulk_insert(self, fields, objs):
                qn = connection.ops.quote_name
                cursor = connection.cursor()

                flds = ', '.join([qn(f) for f in fields])
                values_list = [ r[f] for r in objs for f in fields]
                arg_string = ', '.join([u'(' + ', '.join(['%s']*len(fields)) + ')'] * len(objs))                   
                sql = "INSERT INTO %s (%s) VALUES %s" % (self.model._meta.db_table, flds, arg_string,)             
                cursor.execute(sql, values_list)

More like this

  1. Bulk Insert - updated 5/9/2008 by coolie 5 years, 7 months ago
  2. Authenticate against Active Directory - LDAP (my version) by trebor74hr 4 years, 1 month ago
  3. testdata tag for templates by showell 4 years ago
  4. Management command decorator by eternicode 2 years, 8 months ago
  5. autotranslate po files using google translate by dnordberg 4 years, 8 months ago

Comments

weholt (on February 19, 2011):

You should check out DSE http://pypi.python.org/pypi/dse/0.5.1. It handles much of what you mentioned as gotchas. Like the whole manager thing though. Nice :-).

#

visik7 (on February 21, 2011):

what is the purpose of this thing?

#

(Forgotten your password?)