Fixed #30656 -- Added QuerySet.bulk_update() to the database optimization docs.

This commit is contained in:
daniel a rios 2019-07-26 15:12:29 +02:00 committed by Mariusz Felisiak
parent fe33fdc049
commit 68aeb90160
1 changed files with 31 additions and 0 deletions

View File

@ -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
--------------