Opened 4 years ago
Last modified 4 years ago
#32089 closed Bug
prefetch_related_objects() does not set the `to_attr` on all instances if the first instance in the list already has it set — at Initial Version
Reported by: | Dennis Kliban | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.2 |
Severity: | Normal | Keywords: | |
Cc: | François Freitag, Adam Johnson | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Our project processes instances in a stream. In some cases the instances are repeated. In these cases, we discovered that prefetch_related_objects()
does not set the to_attr
on all of the instances if the first instance in the list already has it set.
When Django determines that the very first instance in the list is_fetched
[0], it does not call into the the prefetch_one_level()
[1]. The attributed specified in the to_attr parameter is only set in the prefetch_one_level()
method[2].
is_fetched is set by looking for the to_attr
attribute on the instance[3].
[0] https://github.com/django/django/blob/stable/2.2.x/django/db/models/query.py#L1605-L1609
[1] https://github.com/django/django/blob/stable/2.2.x/django/db/models/query.py#L1624
[2] https://github.com/django/django/blob/stable/2.2.x/django/db/models/query.py#L1799
[3] https://github.com/django/django/blob/stable/2.2.x/django/db/models/query.py#L1708