From cd124295d882e13cff556fdeb78e6278d10ac6d5 Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Sat, 26 Jun 2021 10:48:38 +0530 Subject: Fixed #32381 -- Made QuerySet.bulk_update() return the number of objects updated. Co-authored-by: Diego Lima --- docs/ref/models/querysets.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'docs/ref/models') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 5dc7a6b5bc..1201800567 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -2221,7 +2221,8 @@ normally supports it). .. method:: bulk_update(objs, fields, batch_size=None) This method efficiently updates the given fields on the provided model -instances, generally with one query:: +instances, generally with one query, and returns the number of objects +updated:: >>> objs = [ ... Entry.objects.create(headline='Entry 1'), @@ -2230,6 +2231,11 @@ instances, generally with one query:: >>> objs[0].headline = 'This is entry 1' >>> objs[1].headline = 'This is entry 2' >>> Entry.objects.bulk_update(objs, ['headline']) + 2 + +.. versionchanged:: 4.0 + + The return value of the number of objects updated was added. :meth:`.QuerySet.update` is used to save the changes, so this is more efficient than iterating through the list of models and calling ``save()`` on each of @@ -2246,6 +2252,10 @@ them, but it has a few caveats: extra query per ancestor. * When an individual batch contains duplicates, only the first instance in that batch will result in an update. +* The number of objects updated returned by the function may be fewer than the + number of objects passed in. This can be due to duplicate objects passed in + which are updated in the same batch or race conditions such that objects are + no longer present in the database. The ``batch_size`` parameter controls how many objects are saved in a single query. The default is to update all objects in one batch, except for SQLite -- cgit v1.3