diff options
| author | Botond Béres <botondus@gmail.com> | 2018-01-13 00:56:16 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-01-12 19:56:16 -0500 |
| commit | 52aa26e6979ba81b00f1593d5ee8c5c73aaa6391 (patch) | |
| tree | bc10cc9bec893f18064b6436a8f43a09ab508e9c /docs | |
| parent | 1b753b2d60230974e7d537ddacbcc1ad23348e7c (diff) | |
Fixed #28231 -- Doc'd that QuerySet.bulk_create() casts objs to a list.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 2003e1bf17..068d1f877b 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1965,6 +1965,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 |
