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
- Bulk Insert - updated 5/9/2008 by coolie 5 years, 7 months ago
- Authenticate against Active Directory - LDAP (my version) by trebor74hr 4 years, 1 month ago
- testdata tag for templates by showell 4 years ago
- Management command decorator by eternicode 2 years, 8 months ago
- autotranslate po files using google translate by dnordberg 4 years, 8 months ago
Comments
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 :-).
#
what is the purpose of this thing?
#