[Django] #32323: Lazy object id fetch on object initialization.

7 views
Skip to first unread message

Django

unread,
Jan 5, 2021, 7:41:48 AM1/5/21
to django-...@googlegroups.com
#32323: Lazy object id fetch on object initialization.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Bartłomiej Nowak |
Type: New | Status: new
feature |
Component: Database | Version: 2.2
layer (models, ORM) | Keywords: bulk_create, fetch
Severity: Normal | id, lazy id fetch,
Triage Stage: | can_return_ids_from_bulk_insert
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Let's assume I am using the Postgres database, so the flag
**can_return_ids_from_bulk_insert** is True.

I would like to do this:
{{{
class ModelA:
pass

class ModelB:
a = models.ForeginKey(ModelA)

a_to_create = []
b_to_create = []
for _ in some_data:
a = ModelA(...)
a_to_create.append(a)
b_to_create.append(
ModelB(a=a, ...)
)

ModelA.objects.bulk_create(a_to_create)
# all objects in the a_to_create list have now ids, becouse of
can_return_ids_from_bulk_insert flag. a_to_create[0].id will return int.
ModelA.objects.bulk_create(b_to_create)
}}}

But when I initialize ModelB(a=a), Django immediately fetches id from
object **a**, which is None in this line, and using this latter, instead
of fetching it (id) from the provided object, when it's going to create.
Why do not fetch this id lazy?

Because of this situation, I have to do an additional loop for every
additional model.


Thank you

--
Ticket URL: <https://code.djangoproject.com/ticket/32323>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 5, 2021, 7:58:55 AM1/5/21
to django-...@googlegroups.com
#32323: Lazy object id fetch on object initialization.
-------------------------------------+-------------------------------------
Reporter: Bartłomiej Nowak | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: bulk_create, fetch | Triage Stage:
id, lazy id fetch, | Unreviewed
can_return_ids_from_bulk_insert |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: New feature => Bug
* resolution: => duplicate


Comment:

Duplicate of #29497, fixed in 10f8b82d195caa3745ba37d9424893763f89653e.

--
Ticket URL: <https://code.djangoproject.com/ticket/32323#comment:1>

Reply all
Reply to author
Forward
0 new messages