I would be willing to make this modification myself, but I actually don't
know if the order of the list is guaranteed to be the same as the order of
the input list. This would be really useful information for developers.
For example, I currently have a piece of code in my codebase that uses
`bulk_create` and then performs an additional check on the output list to
ensure that it is ordered properly. This step would be able to be removed
if I know that the `bulk_create` method outputs the list in the same order
as the input list every time. If someone who knows this information could
send it to me, I can make this change in the documentation myself,
otherwise, someone with that knowledge can make the change.
--
Ticket URL: <https://code.djangoproject.com/ticket/31003>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
AFAIK the model instances are returned in the order they were provided.
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:1>
Comment (by Baptiste Mispelon):
You can see the code for `bulk_create` over there:
https://github.com/django/django/blob/master/django/db/models/query.py#L514
As it is now, it returns the same object as the one that was passed to the
function, so it is in the same order.
However, I'm not sure if this behavior is guaranteed because there don't
seem to be any tests for it (I changed the code to reverse the list before
returning it and the test suite still passes).
It would be good to add a test for the behavior at the same time as you're
documenting it (there's a dedicated file for the `bulk_create` tests
located there:
https://github.com/django/django/blob/master/tests/bulk_create/tests.py).
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:2>
Comment (by felixxm):
`bulk_create()` returns new instances in the same order with PKs only on
PostgreSQL back-end (and this behavior is tested in
`tests.postgres_tests`, e.g. `tests.postgres_tests.test_bulk_update`), on
other back-ends it returns exactly the same objects without PKs so they
are rather useless.
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:3>
* status: new => assigned
* owner: nobody => Peter Vlasveld
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:4>
* owner: Peter Vlasveld => Adam (Chainz) Johnson
* has_patch: 0 => 1
* version: 2.2 => 3.0
* easy: 0 => 1
Comment:
[https://github.com/django/django/pull/12347 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"382af9b14113aaa8821da404933c12053bb394fd" 382af9b1]:
{{{
#!CommitTicketReference repository=""
revision="382af9b14113aaa8821da404933c12053bb394fd"
Fixed #31003 -- Doc'd and tested return value of QuerySet.bulk_create().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"cac9ec73db35a6d38d33f271f4724da486c60e9f" cac9ec73]:
{{{
#!CommitTicketReference repository=""
revision="cac9ec73db35a6d38d33f271f4724da486c60e9f"
Refs #31003 -- Moved note about return value of QuerySet.bulk_create() to
the first paragraph.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:7>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"3eb72ad30f89e6774bfbf0f1223442493955fd74" 3eb72ad3]:
{{{
#!CommitTicketReference repository=""
revision="3eb72ad30f89e6774bfbf0f1223442493955fd74"
[3.2.x] Refs #31003 -- Moved note about return value of
QuerySet.bulk_create() to the first paragraph.
Backport of cac9ec73db35a6d38d33f271f4724da486c60e9f from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31003#comment:8>