Skip to content

Commit f9462f4

Browse files
MisterRiosfelixxm
authored andcommitted
[2.2.x] Fixed #30656 -- Added QuerySet.bulk_update() to the database optimization docs.
Backport of 68aeb90 from master
1 parent b4139ed commit f9462f4

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

docs/topics/db/optimization.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,37 @@ Note that there are a number of :meth:`caveats to this method
367367
<django.db.models.query.QuerySet.bulk_create>`, so make sure it's appropriate
368368
for your use case.
369369

370+
Update in bulk
371+
--------------
372+
373+
.. versionadded:: 2.2
374+
375+
When updating objects, where possible, use the
376+
:meth:`~django.db.models.query.QuerySet.bulk_update()` method to reduce the
377+
number of SQL queries. Given a list or queryset of objects::
378+
379+
entries = Entry.objects.bulk_create([
380+
Entry(headline='This is a test'),
381+
Entry(headline='This is only a test'),
382+
])
383+
384+
The following example::
385+
386+
entries[0].headline = 'This is not a test'
387+
entries[1].headline = 'This is no longer a test'
388+
Entry.objects.bulk_update(entries, ['headline'])
389+
390+
...is preferable to::
391+
392+
entries[0].headline = 'This is not a test'
393+
entries.save()
394+
entries[1].headline = 'This is no longer a test'
395+
entries.save()
396+
397+
Note that there are a number of :meth:`caveats to this method
398+
<django.db.models.query.QuerySet.bulk_update>`, so make sure it's appropriate
399+
for your use case.
400+
370401
Insert in bulk
371402
--------------
372403

0 commit comments

Comments
 (0)