[2.0.x] Fixed #28231 -- Doc'd that QuerySet.bulk_create() casts objs to a list.

Backport of 52aa26e6979ba81b00f1593d5ee8c5c73aaa6391 from master
This commit is contained in:
Botond Béres 2018-01-13 00:56:16 +00:00 committed by Tim Graham
parent cc1bdce674
commit 881f66bc55

View File

@ -1999,6 +1999,21 @@ This has a number of caveats though:
does not retrieve and set the primary key attribute, as ``save()`` does,
unless the database backend supports it (currently PostgreSQL).
* It does not work with many-to-many relationships.
* It casts ``objs`` to a list, which fully evaluates ``objs`` if it's a
generator. The cast allows inspecting all objects so that any objects with a
manually set primary key can be inserted first. If you want to insert objects
in batches without evaluating the entire generator at once, you can use this
technique as long as the objects don't have any manually set primary keys::
from itertools import islice
batch_size = 100
objs = (Entry(headling'Test %s' % i) for i in range(1000))
while True:
batch = list(islice(objs, batch_size))
if not batch:
break
Entry.objects.bulk_create(batch, batch_size)
The ``batch_size`` parameter controls how many objects are created in a single
query. The default is to create all objects in one batch, except for SQLite