Using the models in `tests/prefetch_related/models.py`,
{{{#!python
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.
I'll post a patch fixing this and adding a test case.
--
Ticket URL: <https://code.djangoproject.com/ticket/28723>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* has_patch: 0 => 1
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:1>
Old description:
> 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`,
>
> {{{#!python
> 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.
>
> I'll post a patch fixing this and adding a test case.
New description:
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`,
{{{#!python
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.
I'll post a patch fixing this and adding a test case.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:2>
* type: Uncategorized => Bug
Old description:
> 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`,
>
> {{{#!python
> 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.
>
> I'll post a patch fixing this and adding a test case.
New description:
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`,
{{{#!python
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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:3>
* stage: Unreviewed => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:4>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"514b2c989a948e3c59bda0da0c9427acf643cf5b" 514b2c98]:
{{{
#!CommitTicketReference repository=""
revision="514b2c989a948e3c59bda0da0c9427acf643cf5b"
Fixed #28723 -- Fixed RelatedManager's prefetch_related() cache name.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:5>
Comment (by Mike Hansen):
Thanks Tim!
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:6>
* status: closed => new
* resolution: fixed =>
* stage: Ready for checkin => Accepted
Comment:
I believe this change is causing a regression as the
`_remove_prefetched_objects()` method was not updated to use the new cache
name. See line:
I've created a [https://github.com/django/django/pull/9657 PR] to fix it.
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"8b21878357364a461bae711c4d0011a8f338b160" 8b218783]:
{{{
#!CommitTicketReference repository=""
revision="8b21878357364a461bae711c4d0011a8f338b160"
Refs #28723 -- Fixed stale prefetch_related cache after add/remove.
Regression in 514b2c989a948e3c59bda0da0c9427acf643cf5b.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:8>
* status: new => closed
* resolution: => fixed
--
Ticket URL: <https://code.djangoproject.com/ticket/28723#comment:9>