Skip to content

Commit

Permalink
[2.2.x] Fixed #30656 -- Added QuerySet.bulk_update() to the database …
Browse files Browse the repository at this point in the history
…optimization docs.

Backport of 68aeb90 from master
  • Loading branch information
MisterRios authored and felixxm committed Jul 29, 2019
1 parent b4139ed commit f9462f4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/topics/db/optimization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,37 @@ Note that there are a number of :meth:`caveats to this method
<django.db.models.query.QuerySet.bulk_create>`, so make sure it's appropriate
for your use case.

Update in bulk
--------------

.. versionadded:: 2.2

When updating objects, where possible, use the
:meth:`~django.db.models.query.QuerySet.bulk_update()` method to reduce the
number of SQL queries. Given a list or queryset of objects::

entries = Entry.objects.bulk_create([
Entry(headline='This is a test'),
Entry(headline='This is only a test'),
])

The following example::

entries[0].headline = 'This is not a test'
entries[1].headline = 'This is no longer a test'
Entry.objects.bulk_update(entries, ['headline'])

...is preferable to::

entries[0].headline = 'This is not a test'
entries.save()
entries[1].headline = 'This is no longer a test'
entries.save()

Note that there are a number of :meth:`caveats to this method
<django.db.models.query.QuerySet.bulk_update>`, so make sure it's appropriate
for your use case.

Insert in bulk
--------------

Expand Down

0 comments on commit f9462f4

Please sign in to comment.