How do I perform a batch insert in Django?

前端 未结 5 1685
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 20:12

In mysql, you can insert multiple rows to a table in one query for n > 0:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9), ..., (n-2, n-1, n);
         


        
5条回答
  •  粉色の甜心
    2020-12-23 20:56

    I recently looked for such a thing myself (inspired by QuerySet.update(), as I imagine you are too). To my knowledge, no bulk create exists in the current production framework (1.1.1 as of today). We ended up creating a custom manager for the model that needed bulk-create, and created a function on that manager to build an appropriate SQL statement with the sequence of VALUES parameters.

    Something like (apologies if this does not work... hopefully I've adapted this runnably from our code):

    from django.db import models, connection
    
    class MyManager(models.Manager):
    
        def create_in_bulk(self, values):
            base_sql = "INSERT INTO tbl_name (a,b,c) VALUES "
            values_sql = []
            values_data = []
    
            for value_list in values:
                placeholders = ['%s' for i in range(len(value_list))]
                values_sql.append("(%s)" % ','.join(placeholders))
                values_data.extend(value_list)
    
            sql = '%s%s' % (base_sql, ', '.join(values_sql))
    
            curs = connection.cursor()
            curs.execute(sql, values_data)
    
    class MyObject(models.Model):
        # model definition as usual... assume:
        foo = models.CharField(max_length=128)
    
        # custom manager
        objects = MyManager()
    
    MyObject.objects.create_in_bulk( [('hello',), ('bye',), ('c', )] )
    

    This approach does run the risk of being very specific to a particular database. In our case, we wanted the function to return the IDs just created, so we had a postgres-specific query in the function to generate the requisite number of IDs from the primary key sequence for the table that represents the object. That said, it does perform significantly better in tests versus iterating over the data and issuing separate QuerySet.create() statements.

提交回复
热议问题