How do I perform a batch insert in Django?
Posted
by Thierry Lam
on Stack Overflow
See other posts from Stack Overflow
or by Thierry Lam
Published on 2010-04-16T19:37:08Z
Indexed on
2010/04/16
23:03 UTC
Read the original article
Hit count: 376
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);
Is there a way to achieve the above with Django queryset methods? Here's an example:
values = [(1, 2, 3), (4, 5, 6), ...]
for value in values:
SomeModel.objects.create(first=value[0], second=value[1], third=value[2])
I believe the above is calling an insert query for each iteration of the for loop. I'm looking for a single query, is that possible in Django?
© Stack Overflow or respective owner