Browse Source

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

daniel a rios 5 years ago
parent
commit
68aeb90160
1 changed files with 31 additions and 0 deletions
  1. 31 0
      docs/topics/db/optimization.txt

+ 31 - 0
docs/topics/db/optimization.txt

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