It looks like it would be easy to add the same functionality to
`bulk_update()` since `bulk_update()` simply calls `update()` repeatedly:
https://github.com/django/django/blob/2b4b6c8af0aae8785bc1347cf1be2e8e70fd5ff3/django/db/models/query.py#L568
I.e. the return values could simply be added and returned.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Chris Jerdonek):
Also, if this feature is okayed, maybe the number could be returned in a
way that would permit other useful numbers to be added to the return value
in a backwards-compatible way later on. For example, the number could be
returned as a property of a named tuple with one property. That way, if a
new value is requested in the future, it could be added by adding a new
property to the named tuple.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:1>
* cc: Tom Forbes (added)
* stage: Unreviewed => Accepted
Comment:
> Also, if this feature is okayed, maybe the number could be returned in a
way that would permit other useful numbers to be added to the return value
in a backwards-compatible way later on. For example, the number could be
returned as a property of a named tuple with one property. That way, if a
new value is requested in the future, it could be added by adding a new
property to the named tuple.
Do you have any specific statistics in mind?
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:2>
Comment (by Chris Jerdonek):
Thanks.
Do you have any specific statistics in mind?
Not especially, but more because I don't know what might be available. The
only things that occur to me right now are things like the number of
queries made, and the batch size used for the function call. More
usefully, though, if `update()` were ever to grow the ability to report
not just the number of rows matched but also the number of rows changed,
that would definitely be of interest.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:3>
Comment (by Simon Charette):
> More usefully, though, if update() were ever to grow the ability to
report not just the number of rows matched but also the number of rows
changed, that would definitely be of interest.
Given that would likely require a signature change to `update` if we ever
want to support it I think that making `bulk_update` return the sums of
its `update`s should be good enough for now.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:4>
Comment (by Tom Forbes):
I would second that: it’s good to be future proof, but we’re somewhat
locked in to the current returned result due to update. Let’s just make it
return the updated rows, which seems like a simple and sensible change.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:5>
* easy: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:6>
* owner: nobody => Manav Agarwal
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:7>
Comment (by Tim McCurrach):
I took a look at this yesterday and due to the way bulk_update treats
duplicates in `objs` this might not be quite so straight forward as simply
summing the returned values from repeated `update()` calls. Duplicates
split between batches will be counted twice.
See https://code.djangoproject.com/ticket/32388
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:8>
Comment (by Chris Jerdonek):
There is now an (accepted) issue #32406 to make `update()` return
something different. Given that, might it make sense to reconsider making
`bulk_update()` future-proof now?
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:9>
Comment (by Simon Charette):
`update` will keep returning `int` unless `returning` is specified
otherwise it would break backward compatibility.
Can't this ticket be dedicated to having `bulk_update` returning `int`
when `returning` is not specified?
I don't see what needs to be made here to make `bulk_update` future proof?
How does making `bulk_update -> int` prevent us from having
`bulk_update(returning) -> list` match the signature of `update(returning)
-> list` in the future?
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:10>
* owner: Manav Agarwal => bhavikabadjate901
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:11>
Comment (by bhavikabadjate901):
assign to bhavikabadjate901
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:12>
* cc: Diego Lima (added)
* owner: bhavikabadjate901 => Diego Lima
Comment:
Due to inactivity, I'm claiming this ticket. [comment:11
bhavikabadjate901] please come forward if you have done any work on this!
I'm taking the simplest approach first, which is `bulk_update -> int`. As
stated by [comment:4 Simon Charette]:
> I think that making `bulk_update` return the sums of its `update`s
should be good enough for now.
and [comment:5 Tom Forbes]:
> Let’s just make it return the updated rows
Then, I'll stop and think a bit more about [comment:1 Chris Jerdonek]'s
future-proofing and [comment:8 Tim McCurrach]'s duplicate edge case.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:13>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:14>
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:15>
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:16>
Comment (by Diego Lima):
PR ready for review at [https://github.com/django/django/pull/14125]. I
did not handle duplicates, and did not future-proof `bulk_update`.
I'm keeping an eye on #32406. If it gets patched, I'll update the returned
value on `bulk_update`. If we pin down an API that is generally agreed
upon, I might claim that ticket as well.
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:17>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:18>
* owner: Diego Lima => Abhyudai
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:19>
Comment (by Abhyudai):
new [https://github.com/django/django/pull/14559/ pull request]
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:20>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:21>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:22>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:23>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"cd124295d882e13cff556fdeb78e6278d10ac6d5" cd12429]:
{{{
#!CommitTicketReference repository=""
revision="cd124295d882e13cff556fdeb78e6278d10ac6d5"
Fixed #32381 -- Made QuerySet.bulk_update() return the number of objects
updated.
Co-authored-by: Diego Lima <diego...@lais.huol.ufrn.br>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32381#comment:24>