#31003 closed Cleanup/optimization (fixed)
Suggesting: Documentation update for bulk_create method
Reported by: | Peter Vlasveld | Owned by: | Adam Johnson |
---|---|---|---|
Component: | Documentation | Version: | 3.0 |
Severity: | Normal | Keywords: | bulk_create, database, query, |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
This suggestion relates to the bulk_create
method for creating model objects https://docs.djangoproject.com/en/2.2/ref/models/querysets/#bulk-create. Currently the documentation states what the method does and the caveats associated with its use, but there is no information on what it returns, and in what order the list is returned.
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.
Change History (8)
comment:1 by , 5 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 5 years ago
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).
comment:3 by , 5 years ago
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.
comment:4 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 5 years ago
Easy pickings: | set |
---|---|
Has patch: | set |
Owner: | changed from | to
Version: | 2.2 → 3.0 |
AFAIK the model instances are returned in the order they were provided.