Fixed #30656 -- Added QuerySet.bulk_update() to the database optimization docs.
This commit is contained in:
parent
fe33fdc049
commit
68aeb90160
|
@ -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
|
<django.db.models.query.QuerySet.bulk_create>`, so make sure it's appropriate
|
||||||
for your use case.
|
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
|
Insert in bulk
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue