Opened 7 years ago
Last modified 7 years ago
#28723 closed Bug
RelatedManager.get_prefetch_queryset returns "wrong" cache_name — at Version 3
Reported by: | Mike Hansen | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Currently, RelatedManager.get_prefetch_queryset
returns self.field.related_query_name()
as the cache_name
. In the case where no related_name
has been set on the ForeignKey
, then this does not match with the through_attr
used by get_prefetcher
.
Using the models in tests/prefetch_related/models.py
,
BookWithYear.objects.prefetch_related('bookreview_set')
will use a through_attr
of "bookreview_set"
, but "bookreview"
is what will be placed in _prefetched_objects_cache
.
I think related_manager.field.remote_field.get_accessor_name()
should be used instead.
https://github.com/django/django/pull/9259 is a pull request which fixes this issue
Change History (3)
comment:1 by , 7 years ago
Has patch: | set |
---|
comment:2 by , 7 years ago
Description: | modified (diff) |
---|
comment:3 by , 7 years ago
Description: | modified (diff) |
---|---|
Type: | Uncategorized → Bug |
A pull request for this can be found at https://github.com/django/django/pull/9259
I wasn't sure on the best location for these tests so any guidance would be helpful.